SQL select 使用IN關鍵字

2021-04-18 18:40:10 字數 926 閱讀 5191

sql select 使用in關鍵字

同between關鍵字一樣,in的引入也是為了更方便地限制檢索資料的範圍,靈活使用in關鍵字,可以用簡潔的語句實現結構複雜的查詢。  語法格式為:

表示式 [not]  in  (表示式1 , 表示式2 [,…表示式n])

【例】查詢所有居住在ks、ca、mi或in州的作家。

use  pubs

go select  au_id,au_lname,au_fname

from   authors

where  state  in ('ca','ks','mi','in')

go 如果不使用in關鍵字,這些語句可以使用下面的語句代替:

use  pubs

go  select  au_id,au_lname,au_fname

from   authors

where  state='ca 'or state='ks'or state='mi'or state='in' go

【例】查詢所有不在上述4個州居住的作家。

use  pubs

go select  au_id,au_lname,au_fname

from     authors

where    state not in ('ca','ks','mi','in')

go      

與下面的語句等價:

use  pubs

go select  au_id,au_lname,au_fname

from     authors

where    state<>'ca '

and  state<>'ks'

and   state<>'mi'

and  state<>'in'

go

sql select語句複習

昨天看見了乙個select語句 感覺還可以,拿出來與大家分享一下 當然 字段 表名 僅作參考 供複習用 select dd db table field.field name,dd db table field.ispk,dd db table field.can null,select memo ...

SQL select 語句優化

關於 select from o1,o2.至於為何要進行資料庫優化,就不在這裡重述了。在這裡,將本著謹慎的,簡單的態度,一點一點的講述sql資料查詢語句的優化問題 一,關於索引 使用索引的合理性 條件子句中變數順序應與索引字鍵順序相同。盡可能在join和order by 的字段上建立索引 將最具有限...

SQL SELECT語句的巢狀

問題 現在資料庫中有一張使用者交易表order,其中有userid 使用者id orderid 訂單id amount 訂單金額 paytime 支付時間 請寫出對應的sql語句,查出每個月的新客數 新客指在嚴選首次支付的使用者 當月有複購的新客數,新客當月複購率 公式 當月有複購的新客數 月總新客...