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就要在前面加上括號。也許你子查詢中的條件也要改改才是你要的結果。
轉載請注明出處華閱文章網 » sql語句notin怎么用