1. 程式人生 > >SQL select where 使用IN關鍵字

SQL select where 使用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