日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

常用的 T-SQL 语言

發(fā)布時(shí)間:2023/12/14 数据库 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用的 T-SQL 语言 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • 前言
  • 變量
    • 局部變量的聲明和賦值
  • 通配符
  • 常用的字符串函數(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_2

while

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_table

new_table 表被創(chuàng)建

新表的內(nèi)容

insert into:從一張表向另一張表(該表已經(jīng)存在)插入數(shù)據(jù)
PS:兩張表的屬性類(lèi)型必須一樣

insert into new_table select * from firstselect * from new_table

因?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 cname

having 子句對(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。