常用的 T-SQL 语言
目錄
- 前言
- 變量
- 局部變量的聲明和賦值
- 通配符
- 常用的字符串函數(shù)
- substring
- 日期函數(shù)
- 轉(zhuǎn)換數(shù)據(jù)類(lèi)型函數(shù)
- convert(a,b):強(qiáng)制類(lèi)型轉(zhuǎn)換
- 聚合函數(shù)
- avg,max,sum,count(*)
- 流程控制語(yǔ)句
- 數(shù)據(jù)的插入
- 數(shù)據(jù)的更新
- 數(shù)據(jù)的刪除
- 數(shù)據(jù)的查詢
- 去掉重復(fù)值
- 返回前 n 行(百分比)的數(shù)據(jù)
- 返回合并的多張表的結(jié)果集(表的數(shù)據(jù)類(lèi)型必須一致)
- 條件篩選
- 按照指定序列排序
- 分組
- having 子句對(duì)分組結(jié)果再選擇
- 連接查詢
- 自連接
- 子查詢(嵌套查詢)
前言
代碼所用表(表名:first,second,new_table)
本篇不是詳細(xì)介紹 T-SQL 的語(yǔ)法,而是總結(jié)常用的 T-SQL 語(yǔ)句
變量
局部變量的聲明和賦值
declare @id int --聲明 set @id = 11 --賦值 select @id as showId通配符
% 表示匹配零個(gè)或者多個(gè)字符
select cname from first where cname like '張%'被查詢的表
查詢的結(jié)果
_ 表示匹配一個(gè)字符
select cname from first where cname like '張_'查詢的結(jié)果
[ ] 表示在某一個(gè)范圍內(nèi)的字符
select cid from first where cid like '[0-3]'查詢的結(jié)果
[ ^] 表示不在某一個(gè)范圍內(nèi)的字符
select cid from first where cid like '[^0-3]'查詢的結(jié)果
常用的字符串函數(shù)
substring
select cname = substring(cname,1,1) from first日期函數(shù)
datediff():返回日期之差
select cname as 名字,年齡 = datediff(year,cbirthday,getdate()) from first查詢結(jié)果
轉(zhuǎn)換數(shù)據(jù)類(lèi)型函數(shù)
convert(a,b):強(qiáng)制類(lèi)型轉(zhuǎn)換
a 是要轉(zhuǎn)換的數(shù)據(jù)類(lèi)型,b 是被轉(zhuǎn)換的數(shù)據(jù)類(lèi)型
declare @number int set @number = 3 select convert(char(1),@number) as 轉(zhuǎn)換后聚合函數(shù)
avg,max,sum,count(*)
select count(*) as 行數(shù),avg(convert(int,cage)) as 平均年齡,max(cbirthday) as 最晚出生年月,sum(cid) as id的和 from first流程控制語(yǔ)句
if … else …
declare @number_1 int ,@number_2 int select @number_1 = 1,@number_2 = 2 if @number_1 > @number_2 print @number_1 else print @number_2while
declare @x int select @x = 3 while @x = 3 begin print @x end數(shù)據(jù)的插入
insert 語(yǔ)句:向某個(gè)表中插入數(shù)據(jù)
insert into first values(9,'insert_into',8888,'2005-10-10') --因?yàn)槌錾掌谑?date 類(lèi)型,需要插入字符類(lèi)型隱式轉(zhuǎn)換select into:把一張表的內(nèi)容插入一張新表(該表未被創(chuàng)建)
select * into new_table from first where cid > 0select * from new_tablenew_table 表被創(chuàng)建
新表的內(nèi)容
insert into:從一張表向另一張表(該表已經(jīng)存在)插入數(shù)據(jù)
PS:兩張表的屬性類(lèi)型必須一樣
因?yàn)橹安迦脒^(guò)數(shù)據(jù)(在 select into 中插入過(guò),所以再次插入是兩倍的相同數(shù)據(jù))
數(shù)據(jù)的更新
update first set cid = 100 where cid = 2數(shù)據(jù)的刪除
delete from first where cid = 100 select cid from first數(shù)據(jù)的查詢
去掉重復(fù)值
select distinct cname,* from first -- 去掉了 cname 中重復(fù)的元素返回前 n 行(百分比)的數(shù)據(jù)
select top 3 * from first select top 3 percent * from first返回合并的多張表的結(jié)果集(表的數(shù)據(jù)類(lèi)型必須一致)
select * from first union select * from new_table條件篩選
使用 in 來(lái)篩選
select * from first where cid in (1,2)按照指定序列排序
select * from first order by cage asc分組
PS:group by 在 order by 前面
select cname from first group by cnamehaving 子句對(duì)分組結(jié)果再選擇
select cname from first group by cname having cname like '張%'連接查詢
內(nèi)連接
select * from first inner join second on first.cid = second.cid外連接(此處演示左連接,此外還有右連接和完全外連接)
select * from first left join second on first.cid = second.cid自連接
select a.cid ,b.cage from first a join first b on a.cid = b.cage自連接的實(shí)現(xiàn):通過(guò) from 子句構(gòu)造同一個(gè)表的兩個(gè)實(shí)例 a,b,通過(guò) a,b來(lái)調(diào)用相關(guān)列
SQL 之 自連接
子查詢(嵌套查詢)
select cid from first where cid = (select cid from second where cid =1)總結(jié)
以上是生活随笔為你收集整理的常用的 T-SQL 语言的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [AHK]--显示器输入源快速切换
- 下一篇: linux cmake编译源码,linu