1. 程式人生 > 資料庫 >sqlserver查詢語法

sqlserver查詢語法

基本查詢

select * from 表名

查詢指定列

select 列1名稱,列n名稱 from 表名

查詢指定列名稱 完整查詢

select 表名.列1名稱,表名.列n名稱 from 表名

查詢 條件查詢格式

select * from 表名 where 條件

查詢條件開始===>

比較

等於	=
大於	>
小於	<
大於等於	>=
小於等於 <=
不等於	!=

用法:

列名 運算子 值

例子:

select * from teacher where age =18

select * from teacher where id < 6

模糊查詢

符號:

代表一個		_
任意個		%
字符集		[]
取反字符集	[^]

用法:

列名 like 匹配規則

例子:

找出姓李的資料

select * from teacher where name like ‘李%’

找出名字中包含小的資料

select * from teacher where name like ‘%小%’

找出姓孫的或姓呂的

select * from teacher where name like ‘[孫,呂]%’

找出除了姓孫的,姓呂的以外的所有資料

select * from teacher where name like '[^孫,呂]%'

空查詢 找出欄位值為空的

列名 is null

找出年齡為null的資料

select * from teacher where age is null

空查詢 找出欄位值不為空的

列名 is not null

找出年齡不為空的資料

select * from teacher where age is not null

範圍查詢

注意:會包括最小值與最大值

列名稱 between 最小值 and 最大值

查詢年齡在20到45歲之間的所有的資料

select * from teacher where age between 20 and 45