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

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