1.在Select語(yǔ)句中使用判斷查詢(xún):
功能:計(jì)算條件列表并返回多個(gè)可能結(jié)果表達(dá)式之一。
示例:以判斷user_pass字段值是否為空,為空時(shí)值為yes反之為no查詢(xún)數(shù)據(jù),條件為user_name不為空
select
?case
??when user_pass is null then 'yes' else 'no'
?end as 'user_pass'
?,user_name as 'admin'
from
?admin
where
?user_name is not null
------------------------------------
2.datepart函數(shù)的使用
功能:返回代表指定日期的指定日期部分的整數(shù)。
示例:查詢(xún)2004年與2005年之間的數(shù)據(jù)
select * from admin
where datepart( yyyy,date_time )
?between 2004 and 2005
------------------------------------
3.datediff函數(shù)使用
功能:返回跨兩個(gè)指定日期的日期和時(shí)間邊界數(shù)。
示例:打印日期差
declare @date_time datetime
set @date_time = convert( datetime,'2005-05-06' )
print datediff( dd,@date_time,getdate() )
------------------------------------
4.exists關(guān)鍵字使用
功能:指定一個(gè)子查詢(xún),檢測(cè)行的存在。
示例1:判斷用戶(hù)'admin'是否存在,如存在就返回所有行。
select *
from
?admin
where
?exists( select user_pass from admin where user_name='admin' )
示例2:判斷用戶(hù)'admin'是否存在,如不存在就返回所有行。
select *
from
?admin
where
?not exists( select user_pass from admin where user_name='admin' )
------------------------------------
5.@@IDENTITY
關(guān)鍵字
功能:返回最后插入的標(biāo)識(shí)值。
示例:插入一新行,打印插入的新行的標(biāo)識(shí)ID值。
insert admin( user_name,user_pass,date_time,team_group )
values
?( 'test','test',getdate(),3 )
print @@identity
------------------------------------
6.@@rowcount
關(guān)鍵字
功能:返回受上一語(yǔ)句影響的行數(shù)。
示例1:選擇數(shù)據(jù),返回所選擇的數(shù)據(jù)的行數(shù)
select * from admin
print @@rowcount
示例2:更新數(shù)據(jù),返回被更新數(shù)據(jù)所影響的行數(shù)
update admin set user_name='test' where user_name='zxb'
print @@rowcount
------------------------------------
7.Group by應(yīng)用
功能:
示例1:
?SQL查詢(xún):select type,price from titles where royalty = 10
?結(jié)果:
??type??price
??-----------------------
??business??? ?19.9900
??business??? ?11.9500
??business??? ?19.9900
??popular_comp?20.0000
??psychology? ?21.5900
??psychology? ?7.0000
??psychology? ?19.9900
??psychology? ?7.9900
??trad_cook?? ?20.9500
??trad_cook?? ?14.9900
?Group by 分組查詢(xún):select type,sum(price) as price from titles where royalty=10 group by type
?結(jié)果:
??type??price
??-----------------------
??business??? ?51.9300
??popular_comp?20.0000
??psychology? ?56.5700
??trad_cook?? ?35.9400
?Group by all 分組查詢(xún):select type,sum(price) as price from titles where royalty=10 group by all type
?結(jié)果:
??type??price
??-----------------------
??business??? ?51.9300
??mod_cook??? ?NULL
??popular_comp?20.0000
??psychology? ?56.5700
??trad_cook?? ?35.9400
??UNDECIDED?? ?NULL
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
