sql语句notin怎么用


sql语句not in 和not exist各自的用法和区别

in 和 exists也是很好区别的.

in 是一个集合运算符.

a in {a,c,d,s,d。.}

这个运算中,前面是一个元素,后面是一个集合,集合中的元素类型是和前面的元素一样的.

而exists是一个存在判断,如果后面的查询中有结果,则exists为真,否则为假.

in 运算用在语句中,它后面带的select 一定是选一个字段,而不是select *.

比如说你要判断某班是否存在一个名为”小明”的学生,你可以用in 运算:

“小明” in (select sname from student)

这样(select sname from student) 返回的是一个全班姓名的集合,in用于判断”小明”是否为此集合中的一个数据;

同时,你也可以用exists语句:

exists (select * from student where sname=”小明”)

请教一个NOT IN 的SQL语句

SELECT TOP 15 f_id,f_title,f_pubtime FROM t_Article

WHERE (charindex(‘,89,’,f_class) > 0

OR charindex(‘,90,’,f_class) > 0

OR charindex(‘,91,’,f_class) > 0

OR charindex(‘,92,’,f_class) > 0

OR charindex(‘,93,’,f_class) > 0 )

AND f_Audit = ‘1’ AND f_id not in (

SELECT TOP 4 f_id FROM t_Article

WHERE charindex(‘,89,’,f_class) > 0

OR charindex(‘,90,’,f_class) > 0

OR charindex(‘,91,’,f_class) > 0

OR charindex(‘,92,’,f_class) > 0

OR charindex(‘,93,’,f_class) > 0

AND f_Audit = ‘1’

AND f_imagesmall != ” ORDER BY f_pubtime DESC)

ORDER BY f_pubtime DESC

一楼说的对,就是AND的级别比OR高,你要先运行OR就要在前面加上括号。也许你子查询中的条件也要改改才是你要的结果。