sql in; exists; all;any; having;between

1、in

  in主要查询是一个范围,例如查一组数为1~10的ID数,我只需要查2,3, 6这里则是:

select ID from A where ID in(2, 3, 6)

2、exists

  查询A表里面和B表里面ID关联的数据

  select * from A where exists (select * from B where A.ID = B.AID)

  即:select * from A m left join B s on m.ID = s.ID

in与exists的区别,数据量小的用in ,大数据的用exists

 

3、all

  查询ID大于10的数据,前提是条件里的数据必须全部都满足

  例如有一组数据1~10,查询小于5的数据

  select * from A where 5 >all(select ID from A)

  查出的为空

4、any

  查询ID大于10的数据,前提是条件里的数据必须主要满足一项就全部查出

  select * from A where 4 > any(select ID from A)

5、having

  having与where的区别只要是where后面不能跟着聚合函数一起使用,而having必须跟着group by聚合函数后面一起使用.

  例如:查A表里面是否有相同的数据的ID

  select ID from A group by id having count(id) > 1

 

 

posted @ 2013-10-24 16:30  淡 淡  阅读(219)  评论(0)    收藏  举报