首页 > 数据库技术 > 详细

SQL:找到一个关于all some any的用法,可在SSMS里看效果

时间:2014-07-12 00:46:07      阅读:353      评论:0      收藏:0      [点我收藏+]

SET nocount ON

USE tempdb
go

IF ( OBJECT_ID(‘t1‘) IS NOT NULL )
DROP TABLE t1
CREATE TABLE t1 ( n INT )
INSERT INTO t1
SELECT 2
UNION
SELECT 3

IF ( OBJECT_ID(‘t2‘) IS NOT NULL )
DROP TABLE t2
CREATE TABLE t2 ( n INT )
INSERT INTO t2
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4

-- t1表数据 2,3
-- t2表数据 1,2,3,4
SELECT *
FROM t1
SELECT *
FROM t2
-- ‘>all‘ 表示:t2表中列n的数据大于t1表中列n的数据的数,结果只有4.
SELECT *
FROM t2
WHERE n > ALL ( SELECT n
FROM t1 )
--4
SELECT *
FROM t2
WHERE n > ANY ( SELECT n
FROM t1 )
--3,4
SELECT *
FROM t2
WHERE n > SOME ( SELECT n
FROM t1 )
--3,4

SELECT *
FROM t2
WHERE n = ALL ( SELECT n
FROM t1 )
--无数据
SELECT *
FROM t2
WHERE n = ANY ( SELECT n
FROM t1 )
--2,3
SELECT *
FROM t2
WHERE n = SOME ( SELECT n
FROM t1 )
--2,3

SELECT *
FROM t2
WHERE n < ALL ( SELECT n
FROM t1 )
--1
SELECT *
FROM t2
WHERE n < ANY ( SELECT n
FROM t1 )
--1,2
SELECT *
FROM t2
WHERE n < SOME ( SELECT n
FROM t1 )
--1,2

SELECT *
FROM t2
WHERE n <>ALL ( SELECT n
FROM t1 )
--1,4
SELECT *
FROM t2
WHERE n <>ANY ( SELECT n
FROM t1 )
--1,2,3,4
SELECT *
FROM t2
WHERE n <>SOME ( SELECT n
FROM t1 )
--1,2,3,4
SET nocount OFF

SQL:找到一个关于all some any的用法,可在SSMS里看效果,布布扣,bubuko.com

SQL:找到一个关于all some any的用法,可在SSMS里看效果

原文:http://www.cnblogs.com/i124q29/p/3832729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!