码迷,mamicode.com
首页 > 数据库 > 详细

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

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

标签:数据   io   sql   re   c   table   

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里看效果

标签:数据   io   sql   re   c   table   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!