SQL select 使用IN关键字

来源:互联网 发布:比特币 编程语言 编辑:程序博客网 时间:2024/06/05 18:51

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

原创粉丝点击