SQL语句中遇到的坑

来源:互联网 发布:betterzip 3.1.2 mac 编辑:程序博客网 时间:2024/05/29 16:09

1. 判断某字段不是NULL,应该用 

IS NOT NULL, 而不是 <> NULL


2. 结果中取唯一的值,或者说合并相同的值,用

Distinct,不需要group by.


3. 两个条件,一个字段存在,另一个字段不存在,可以用and not in比如:

查询只有诺贝尔物理奖没有诺贝尔化学奖的年份:

select distinct yr from nobel where subject = 'Physics' and yr not in (select distinct yr from nobel where subject = 'Chemistry')

http://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial


4. all vs any

--All:对所有数据都满足条件,整个条件才成立,例如:5大于所有返回的id
select *
from #A
where 5>All(select id from #A)
go

--Any:只要有一条数据满足条件,整个条件成立,例如:3大于1,2
select *
from #A
where 3>any(select id from #A)
go
--Some和Any一样

http://www.cnblogs.com/Myhsg/archive/2009/12/17/1626738.html



0 0
原创粉丝点击