常用的SQL例句 數(shù)據(jù)庫開發(fā)所需知識(shí)
更新時(shí)間:2011年11月26日 23:58:53 作者:
常用的SQL例句全部懂了,你的數(shù)據(jù)庫開發(fā)所需知識(shí)就夠用了
--查看學(xué)生表的全部數(shù)據(jù)
select * from studio
--插入一個(gè)新的學(xué)生信息
insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黃蘭淇",0,36,'南充','13943943334')
--查看class全部數(shù)據(jù)
select * from class
--向class表增加兩條條數(shù)據(jù)
insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新電實(shí)訓(xùn)班','GXA-ncs-001','2008-03-11','都是很優(yōu)秀的朋友')
insert into class(cl_class,cl_coding,cl_o_time) values('阿壩師專實(shí)訓(xùn)班','GXA-ABSZ-001','2008-03-11')
--更新一條的數(shù)據(jù) 條件的重要性
update class set cl_remark='真的是不錯(cuò)' where cl_id=5
--刪除一條數(shù)據(jù) 條件的重要性
delete from class where cl_id=7
--修改列標(biāo)題
select cl_id as '班級(jí)主鍵',cl_class as '班級(jí)名稱' from class
select 名字=st_name from studio
--使用文字串
select '名字是:',st_name from studio
--=============條件稍微復(fù)雜點(diǎn)的查增刪改==============
--主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null
--查詢cl_id 大于 1 的所有信息
select * from class where cl_id>1
--使用 or
select * from class where cl_id<>10 or cl_class='百杰一班'
--使用and
select * from class where cl_id<>10 and cl_class='百杰一班'
--使用like 和 %
select * from class where cl_class like '百杰%'
select * from class where cl_remark like '%上午%'
--使用 between
select * from class where cl_id between 3 and 5
--使用 between 配合上 not
select * from class where cl_id not between 3 and 5
--使用 is not null
select * from class where cl_remark is not null
--使用 in
select * from class where cl_class in('千星一班','百杰二班')
--=================使用數(shù)學(xué)運(yùn)算符======================
--主要涉及到 + = * \
--查詢Java相關(guān)課程分別要上多少周 按照每周5天,每天6節(jié)課來計(jì)算
select '結(jié)果'=co_num/5/6 from course where co_name in ('Java基礎(chǔ)','Java項(xiàng)目入門')
--==================使用匯總函數(shù) ========================
--涉及到COUNT SUM AVG MAX MIN
--查詢課時(shí)數(shù)小于50的課程一共有多少門
select count(*) from course where co_num<50
--查詢所有課程一共多少課時(shí)
select sum(co_num) from course
--計(jì)算全部課時(shí)費(fèi),假設(shè)每節(jié)課50塊錢
select sum(co_num)*50 from course
--查詢課時(shí)最少的課程
select min(co_num) from course
--查詢課時(shí)最多的課程
select max(co_num) from course
--查詢平均每門課多少課時(shí)
select avg(co_num) from course
--=================使用數(shù)學(xué)函數(shù)=============================
--包括求絕對(duì)值函數(shù)ABS函數(shù)、求圓周率函數(shù)PI()、求正玄值SIN()函數(shù)、求指數(shù)函數(shù)EXP()等。
--查詢每門課的正弦值
select sin(co_num) from course
--查詢每門課的絕對(duì)值
select abs(co_num) from course
--查詢每門課課時(shí)數(shù) 乘以 圓周率 ,具體有什么用我也不知道,反正這好像絕對(duì)是8.5桿子都打不到的
select pi()*co_num from course
--查詢每門課的指數(shù)
select exp(co_num) from course
--隨機(jī)返回5個(gè)隨機(jī)生成的數(shù)(返回的是0~1之間的隨機(jī)float值)
declare @i tinyint
set @i=1
while @i<=5
begin
select rand(@i) as '隨機(jī)生成的數(shù)' , @i as '當(dāng)前值'
set @i=@i+1
end
--返回?cái)?shù)字表達(dá)式并四舍五入為指定的長度或精度 - ROUND
select round(345.456,-1) as '參數(shù)為-1'
, round(345.456,-2,1) as '參數(shù)為-2'
, round(345.456,0) as '參數(shù)為0'
, round(345.456,1) as '參數(shù)為1'
, round(345.456,2) as '參數(shù)為2'
--================使用日期函數(shù)======================
--DAY()、MONTH()、YEAR()——返回指定日期的天數(shù)、月數(shù)、年數(shù);
select day(cl_s_time) as '日' from class --返回天
select '月'=month(cl_s_time) from class --返回月
select '年'=year(cl_s_time) from class --返回年
--DATEADD(datepart,number,date)——在日期上增加給定日期類型的數(shù)量;
select dateadd(yyyy,4,cl_s_time) as '增加4年后' from class --datepart - 年份
yy、yyyy
select dateadd(q,2,cl_s_time) as '增加2季度后' from class
--datepart - 季度
qq、q
select dateadd(mm,3,cl_s_time) as '增加3月度后' from class
--datepart - 月份
mm、m
--datepart - 每年的某一日
dy、y
--datepart - 日期
dd、d
--datepart - 星期
wk、ww
--datepart - 小時(shí)
hh
--datepart - 分鐘
mi、n
--datepart - 秒
ss、s
--datepart - 毫秒
ms
--DATEDIFF(datepart,date1,date2)——獲取兩個(gè)日期之間給定的日期類型的數(shù)量差(整個(gè)函數(shù)結(jié)果是date2-date1);
select datediff(mm,cl_s_time,cl_o_time) as '共持續(xù)月' from class
--datepart(datepart,date)——在給定日期基礎(chǔ)上返回指定日期類型的值(整數(shù));
--其實(shí)這個(gè)等同于DAY、MONTH、和 YEAR 函數(shù)
select datepart(dd,cl_s_time) as '日期' from class
--GETDATE()——返回當(dāng)前日期和時(shí)間。我們?cè)谠O(shè)計(jì)數(shù)據(jù)庫的時(shí)候,通常也可能把他作為默認(rèn)值
update class set cl_s_time=getdate() where cl_id=6
select * from class
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06
Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16
Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06
Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06
Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06
Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06
Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06
Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46
Select CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AM
Select CONVERT(varchar(100), GETDATE(), 10): 05-16-06
Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16
Select CONVERT(varchar(100), GETDATE(), 12): 060516
Select CONVERT(varchar(100), GETDATE(), 13): 16 05 2006 10:57:46:937
Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967
Select CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47
Select CONVERT(varchar(100), GETDATE(), 21): 2006-05-16 10:57:47.157
Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AM
Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16
Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47
Select CONVERT(varchar(100), GETDATE(), 25): 2006-05-16 10:57:47.250
Select CONVERT(varchar(100), GETDATE(), 100): 05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 101): 05/16/2006
Select CONVERT(varchar(100), GETDATE(), 102): 2006.05.16
Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006
Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006
Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006
Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006
Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006
Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49
Select CONVERT(varchar(100), GETDATE(), 109): 05 16 2006 10:57:49:437AM
Select CONVERT(varchar(100), GETDATE(), 110): 05-16-2006
Select CONVERT(varchar(100), GETDATE(), 111): 2006/05/16
Select CONVERT(varchar(100), GETDATE(), 112): 20060516
Select CONVERT(varchar(100), GETDATE(), 113): 16 05 2006 10:57:49:513
Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547
Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49
Select CONVERT(varchar(100), GETDATE(), 121): 2006-05-16 10:57:49.700
Select CONVERT(varchar(100), GETDATE(), 126): 2006-05-16T10:57:49.827
Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? 1427 10:57:49:907AM
Select CONVERT(varchar(100), GETDATE(), 131): 18/04/1427 10:57:49:920AM
--=============使用字符串函數(shù)=====================
--字符串鏈接運(yùn)算符
select '結(jié)果顯示' = '班級(jí)名稱是:' + cl_class + ',班級(jí)編號(hào)是:' + cl_coding from class
--使用SUBSTRING函數(shù)截取字符串
select substring(cl_class,1,4) from class
--從字符串的左邊開始返回3個(gè)字符
select left(cl_class,3) from class
--同理,返回右邊的
select right(cl_class,3) from class
--返回值的字符數(shù)
select len(cl_class) from class
--替換
select replace(cl_class,'實(shí)訓(xùn)','強(qiáng)化') from class
--==============使用系統(tǒng)函數(shù)====================
select host_id()
--返回工作站標(biāo)識(shí)號(hào)
select host_name()
--返回工作站所運(yùn)行的計(jì)算機(jī)名稱
select db_id()
select db_name()
select object_id('Stu_course_ADD')
--通過名稱得到這個(gè)服務(wù)器對(duì)象的服務(wù)器ID
select object_name(151671588)
--同上相反
--=======學(xué) 云 網(wǎng)-天轟穿-[url]www.ixueyun.co m[/url]===使用其他子句====學(xué) 云網(wǎng)- 天轟 穿-[url]www.ixueyun.com[/url]=========
--首先是 order by 功能 - 排序
select * from studio order by st_name
--多排序條件
select * from studio order by st_name DESC,st_age DESC,st_sex DESC
--有條件,主要是看下條件和子句的位置
select * from studio where cl_id=1 order by st_name
--GROUP BY 子句 功能 - 分組統(tǒng)計(jì)
select cl_id as '班級(jí)編號(hào)',count(*) as '人數(shù)' from studio group by cl_id
--按宿舍統(tǒng)計(jì)年齡平均值
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id
--多分組
select ho_id as '宿舍編號(hào)',cl_id as '班級(jí)編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id,cl_id
--有條件,主要是看下條件和子句的位置
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio where cl_id=1 group by ho_id
--使用 having 子句 功能 - 指定組或者聚合的搜索條件,通常與group by 子句一起使用,完成分組查詢后再進(jìn)步篩選
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id having avg(st_age)>35
--多條件
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id having avg(st_age)>35 and ho_id>2
--===========聯(lián)合查詢=======學(xué) 云 網(wǎng)-天轟穿-[url]www.ixueyun.com[/url]======
--使用union子句的查詢稱為聯(lián)合查詢,功能:將兩個(gè)以上的查詢結(jié)果集組合為一個(gè)單個(gè)結(jié)果集,該集中包括所有集中的全部行數(shù)據(jù)
--下面我們嘗試將多個(gè)查詢聯(lián)合起來
select * from studio where cl_id=1
union
select * from studio where ho_id=1
union
select * from studio where st_age>=30
--下面我們繼續(xù)利用上面的例題,增加上 All 看下效果
select * from studio where cl_id=1
union all
select * from studio where ho_id=1
union all
select * from studio where st_age>=30
--再繼續(xù)利用,給他加上排序
select * from studio where cl_id=1
union all
select * from studio where ho_id=1
union all
select * from studio where st_age>=30
order by st_id
--===========連接查詢==================
--連接查詢,功能 - 將多個(gè)表中的數(shù)據(jù)查詢出來放在一起
--內(nèi)連接:使用比較運(yùn)算符=><....等進(jìn)行表間某些數(shù)據(jù)庫的比較操作,并列出這些表中與連接條件相匹配的數(shù)據(jù)行
--等值連接,當(dāng)然就是用等號(hào)了,毛病,這也要問
select * from studio inner join class on studio.cl_id = class.cl_id
--指明要查詢的列(江湖上又稱自然連接),并排序
select st_id as '編號(hào)',st_name as '學(xué)生姓名',cl_class as '班級(jí)名稱' from studio inner join class on studio.cl_id = class.cl_id order by st_id
--使用表別名
select st.st_name as '學(xué)生姓名',st.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱' from studio as st inner join class as cl on st.cl_id = cl.cl_id
--不等連接,這個(gè)問題很好笑,既然使用等號(hào)的是等值連接,那么不等值你說是不是應(yīng)該是非等于以外的呢?
--下面我們?cè)龠B接第三個(gè)表,看下是怎么搞滴
select st.st_name as '學(xué)生姓名',st.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱' ,ho.ho_coding as '所在宿舍編號(hào)'
from studio as st inner join class as cl
on st.cl_id = cl.cl_id
inner join hostel as ho
on st.ho_id=ho.ho_id
--我們?cè)俳o他加個(gè)條件看下
--where st.cl_id>2
--再給他個(gè)排序
--order by st.st_id
--外連接:
--與內(nèi)連接不同的是,內(nèi)連接至少要有一個(gè)同屬于兩個(gè)表的行符合連接條件時(shí)才會(huì)返回行,外連接會(huì)返回符合任意條件的行
--他的表有主從之分,他用主表中的每行去匹配從表中的,與內(nèi)連不同的是,他不會(huì)丟棄沒有匹配的行,而是填充null給從結(jié)果集
--左外連接
select st.st_id as '學(xué)生編號(hào)', st.st_name as '學(xué)生姓名',cl.cl_id as '班級(jí)編號(hào)',cl_class as '班級(jí)名稱'
from studio as st left outer join class as cl
on st.cl_id=cl.cl_id
where cl.cl_id>2
--多表
select tka.te_co_id as '課程安排編號(hào)'
,cl.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時(shí)數(shù)'
,te.te_name as '老師姓名'
from te_kc_ap as tka left outer join class as cl
on tka.cl_id=cl.cl_id
left outer join
course as co
on tka.co_id=co.co_id
left outer join
teacher as te
on tka.te_id=te.te_id
--====================右外連結(jié) =======================
select st.st_id as '學(xué)生編號(hào)', st.st_name as '學(xué)生姓名',cl.cl_id as '班級(jí)編號(hào)',cl_class as '班級(jí)名稱'
from studio as st right outer join class as cl
on st.cl_id=cl.cl_id
where cl.cl_id>2
--多表
select tka.te_co_id as '課程安排編號(hào)'
,cl.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時(shí)數(shù)'
,te.te_name as '老師姓名'
from te_kc_ap as tka
right outer join class as cl
on
tka.cl_id=cl.cl_id
right outer join teacher te
on
tka.te_id=te.te_id
right outer join course co
on
tka.co_id=co.co_id
--========完全連接==============
select st.st_id as '學(xué)生編號(hào)', st.st_name as '學(xué)生姓名',cl.cl_id as '班級(jí)編號(hào)',cl_class as '班級(jí)名稱'
from studio as st full outer join class as cl
on st.cl_id=cl.cl_id
order by st.st_id
--多表
select tka.te_co_id as '課程安排編號(hào)'
,cl.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時(shí)數(shù)'
,te.te_name as '老師姓名'
from te_kc_ap as tka
full outer join class as cl
on
tka.cl_id=cl.cl_id
full outer join teacher te
on
tka.te_id=te.te_id
full outer join course co
on
tka.co_id=co.co_id
--==========交叉連接================
--該方式在不帶where子句時(shí),返回的是兩個(gè)表中所有數(shù)據(jù)行的笛卡爾積(第一個(gè)表中的行乘以第二個(gè)表中的行)
--用學(xué)生和班級(jí)表做交叉查詢
select st_name,cl_class from studio cross join class
select st_name,cl_class from studio,class
select st_name,cl_class from studio cross join class
--=========自連接===
-----------------先臨時(shí)創(chuàng)建一個(gè)表-------------
create table zone(
id int primary key identity(1,1) not null,
z_zone varchar(30),
z_id int references zone(id))
--大家試下,這里是否可以給個(gè)默認(rèn)值
select * from zone
insert into zone(z_zone) values('北京')
insert into zone(z_zone,z_id) values('北京',4)
insert into zone(z_zone) values('四川')
insert into zone(z_zone,z_id) values('成都',6)
insert into zone(z_zone,z_id) values('綿陽',6)
insert into zone(z_zone) values('江蘇')
insert into zone(z_zone,z_id) values('南京',10)
insert into zone(z_zone,z_id) values('蘇州',10)
insert into zone(z_zone,z_id) values('無錫',10)
insert into zone(z_zone,z_id) values('常州',10)
----------------------------------------------
--看下自連接的一般用處
select a.z_zone,b.z_zone from zone as a inner join zone as b on a.z_id=b.id
--擴(kuò)展應(yīng)用下
select b.z_zone,count(a.z_zone) as '轄區(qū)數(shù)' from zone as a inner join zone as b on a.z_id=b.id group by b.z_zone
--簡單說就是自己連接自己,換言之對(duì)同一個(gè)表進(jìn)行連接操作
select a.st_name,a.st_add,b.st_name,b.st_add from studio as a inner join studio as b on a.st_add=b.st_add
--我們發(fā)現(xiàn)有人等于自己,那么增加一個(gè)條件
select a.st_name,a.st_add,b.st_name,b.st_add from studio as a inner join studio as b on a.st_add=b.st_add and a.st_name!=b.st_name
--====學(xué) 云網(wǎng)-天轟穿-[url]www.ixueyun.com[/url]==子查詢============
--在一個(gè)SQL語句中鑲?cè)肓硪粋€(gè)SQL語句教鑲套查詢,而被鑲?cè)氲倪@個(gè)SQL語句就被江湖人稱子查詢。是處理多表操作的附加方法
--子查詢也稱內(nèi)部查詢,而包含子查詢的Select語句被誠為外部查詢,子查詢自身可以包括一個(gè)或者多個(gè)子查詢,也可以鑲套任意數(shù)量的子查詢
--使用in的子查詢
select * from studio where cl_id in (select cl_id from class where cl_id>2)
--使用 not in
select * from studio where cl_id not in (select cl_id from class where cl_id>2)
--使用比較運(yùn)算符的子查詢 -- any 表示子查詢中任意的值 all 表示子查詢中的每個(gè)值
--使用any
select * from class where cl_id>any(select cl_id from studio where st_age>30)
--使用all
select * from class where cl_id>all(select cl_id from studio where st_age>30)
--============一個(gè)分頁的SQL語句========
select top 3 * from studio
where st_id>all(select top 3 st_id from studio order by st_id)
order by st_id
--使用 exists ,該關(guān)鍵字引入一個(gè)子查詢的時(shí)候基本上是對(duì)數(shù)據(jù)進(jìn)行一次是否存在的測(cè)試
--我們查詢那些人所在的班級(jí)是編號(hào)為 1 的
select * from studio where exists(select cl_id from class where studio.cl_id=class.cl_id and class.cl_id=1)
--使用 not exists
select * from studio where not exists(select * from class where studio.cl_id=class.cl_id and class.cl_id=1) order by st_id
--基于查詢生成新的表
select st_name into class_3 from studio where cl_id=3
--將數(shù)據(jù)批量插入一個(gè)表中
insert into class_3 select st_name from studio where cl_id=4
-----------------------sql 編程--------------
declare @max int;
--申明一個(gè)變量@max
set @max=1;
--為變量@max賦值
while @max<10
--如果@max小于10就進(jìn)入循環(huán)
begin
set @max=@max+1;--每次循環(huán)就給@max加1
print @max;
--打印當(dāng)前@max的值
end
print '終于循環(huán)完了';
select * from studio
--插入一個(gè)新的學(xué)生信息
insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黃蘭淇",0,36,'南充','13943943334')
--查看class全部數(shù)據(jù)
select * from class
--向class表增加兩條條數(shù)據(jù)
insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新電實(shí)訓(xùn)班','GXA-ncs-001','2008-03-11','都是很優(yōu)秀的朋友')
insert into class(cl_class,cl_coding,cl_o_time) values('阿壩師專實(shí)訓(xùn)班','GXA-ABSZ-001','2008-03-11')
--更新一條的數(shù)據(jù) 條件的重要性
update class set cl_remark='真的是不錯(cuò)' where cl_id=5
--刪除一條數(shù)據(jù) 條件的重要性
delete from class where cl_id=7
--修改列標(biāo)題
select cl_id as '班級(jí)主鍵',cl_class as '班級(jí)名稱' from class
select 名字=st_name from studio
--使用文字串
select '名字是:',st_name from studio
--=============條件稍微復(fù)雜點(diǎn)的查增刪改==============
--主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null
--查詢cl_id 大于 1 的所有信息
select * from class where cl_id>1
--使用 or
select * from class where cl_id<>10 or cl_class='百杰一班'
--使用and
select * from class where cl_id<>10 and cl_class='百杰一班'
--使用like 和 %
select * from class where cl_class like '百杰%'
select * from class where cl_remark like '%上午%'
--使用 between
select * from class where cl_id between 3 and 5
--使用 between 配合上 not
select * from class where cl_id not between 3 and 5
--使用 is not null
select * from class where cl_remark is not null
--使用 in
select * from class where cl_class in('千星一班','百杰二班')
--=================使用數(shù)學(xué)運(yùn)算符======================
--主要涉及到 + = * \
--查詢Java相關(guān)課程分別要上多少周 按照每周5天,每天6節(jié)課來計(jì)算
select '結(jié)果'=co_num/5/6 from course where co_name in ('Java基礎(chǔ)','Java項(xiàng)目入門')
--==================使用匯總函數(shù) ========================
--涉及到COUNT SUM AVG MAX MIN
--查詢課時(shí)數(shù)小于50的課程一共有多少門
select count(*) from course where co_num<50
--查詢所有課程一共多少課時(shí)
select sum(co_num) from course
--計(jì)算全部課時(shí)費(fèi),假設(shè)每節(jié)課50塊錢
select sum(co_num)*50 from course
--查詢課時(shí)最少的課程
select min(co_num) from course
--查詢課時(shí)最多的課程
select max(co_num) from course
--查詢平均每門課多少課時(shí)
select avg(co_num) from course
--=================使用數(shù)學(xué)函數(shù)=============================
--包括求絕對(duì)值函數(shù)ABS函數(shù)、求圓周率函數(shù)PI()、求正玄值SIN()函數(shù)、求指數(shù)函數(shù)EXP()等。
--查詢每門課的正弦值
select sin(co_num) from course
--查詢每門課的絕對(duì)值
select abs(co_num) from course
--查詢每門課課時(shí)數(shù) 乘以 圓周率 ,具體有什么用我也不知道,反正這好像絕對(duì)是8.5桿子都打不到的
select pi()*co_num from course
--查詢每門課的指數(shù)
select exp(co_num) from course
--隨機(jī)返回5個(gè)隨機(jī)生成的數(shù)(返回的是0~1之間的隨機(jī)float值)
declare @i tinyint
set @i=1
while @i<=5
begin
select rand(@i) as '隨機(jī)生成的數(shù)' , @i as '當(dāng)前值'
set @i=@i+1
end
--返回?cái)?shù)字表達(dá)式并四舍五入為指定的長度或精度 - ROUND
select round(345.456,-1) as '參數(shù)為-1'
, round(345.456,-2,1) as '參數(shù)為-2'
, round(345.456,0) as '參數(shù)為0'
, round(345.456,1) as '參數(shù)為1'
, round(345.456,2) as '參數(shù)為2'
--================使用日期函數(shù)======================
--DAY()、MONTH()、YEAR()——返回指定日期的天數(shù)、月數(shù)、年數(shù);
select day(cl_s_time) as '日' from class --返回天
select '月'=month(cl_s_time) from class --返回月
select '年'=year(cl_s_time) from class --返回年
--DATEADD(datepart,number,date)——在日期上增加給定日期類型的數(shù)量;
select dateadd(yyyy,4,cl_s_time) as '增加4年后' from class --datepart - 年份
yy、yyyy
select dateadd(q,2,cl_s_time) as '增加2季度后' from class
--datepart - 季度
qq、q
select dateadd(mm,3,cl_s_time) as '增加3月度后' from class
--datepart - 月份
mm、m
--datepart - 每年的某一日
dy、y
--datepart - 日期
dd、d
--datepart - 星期
wk、ww
--datepart - 小時(shí)
hh
--datepart - 分鐘
mi、n
--datepart - 秒
ss、s
--datepart - 毫秒
ms
--DATEDIFF(datepart,date1,date2)——獲取兩個(gè)日期之間給定的日期類型的數(shù)量差(整個(gè)函數(shù)結(jié)果是date2-date1);
select datediff(mm,cl_s_time,cl_o_time) as '共持續(xù)月' from class
--datepart(datepart,date)——在給定日期基礎(chǔ)上返回指定日期類型的值(整數(shù));
--其實(shí)這個(gè)等同于DAY、MONTH、和 YEAR 函數(shù)
select datepart(dd,cl_s_time) as '日期' from class
--GETDATE()——返回當(dāng)前日期和時(shí)間。我們?cè)谠O(shè)計(jì)數(shù)據(jù)庫的時(shí)候,通常也可能把他作為默認(rèn)值
update class set cl_s_time=getdate() where cl_id=6
select * from class
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06
Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16
Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06
Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06
Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06
Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06
Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06
Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46
Select CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AM
Select CONVERT(varchar(100), GETDATE(), 10): 05-16-06
Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16
Select CONVERT(varchar(100), GETDATE(), 12): 060516
Select CONVERT(varchar(100), GETDATE(), 13): 16 05 2006 10:57:46:937
Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967
Select CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47
Select CONVERT(varchar(100), GETDATE(), 21): 2006-05-16 10:57:47.157
Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AM
Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16
Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47
Select CONVERT(varchar(100), GETDATE(), 25): 2006-05-16 10:57:47.250
Select CONVERT(varchar(100), GETDATE(), 100): 05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 101): 05/16/2006
Select CONVERT(varchar(100), GETDATE(), 102): 2006.05.16
Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006
Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006
Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006
Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006
Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006
Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49
Select CONVERT(varchar(100), GETDATE(), 109): 05 16 2006 10:57:49:437AM
Select CONVERT(varchar(100), GETDATE(), 110): 05-16-2006
Select CONVERT(varchar(100), GETDATE(), 111): 2006/05/16
Select CONVERT(varchar(100), GETDATE(), 112): 20060516
Select CONVERT(varchar(100), GETDATE(), 113): 16 05 2006 10:57:49:513
Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547
Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49
Select CONVERT(varchar(100), GETDATE(), 121): 2006-05-16 10:57:49.700
Select CONVERT(varchar(100), GETDATE(), 126): 2006-05-16T10:57:49.827
Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? 1427 10:57:49:907AM
Select CONVERT(varchar(100), GETDATE(), 131): 18/04/1427 10:57:49:920AM
--=============使用字符串函數(shù)=====================
--字符串鏈接運(yùn)算符
select '結(jié)果顯示' = '班級(jí)名稱是:' + cl_class + ',班級(jí)編號(hào)是:' + cl_coding from class
--使用SUBSTRING函數(shù)截取字符串
select substring(cl_class,1,4) from class
--從字符串的左邊開始返回3個(gè)字符
select left(cl_class,3) from class
--同理,返回右邊的
select right(cl_class,3) from class
--返回值的字符數(shù)
select len(cl_class) from class
--替換
select replace(cl_class,'實(shí)訓(xùn)','強(qiáng)化') from class
--==============使用系統(tǒng)函數(shù)====================
select host_id()
--返回工作站標(biāo)識(shí)號(hào)
select host_name()
--返回工作站所運(yùn)行的計(jì)算機(jī)名稱
select db_id()
select db_name()
select object_id('Stu_course_ADD')
--通過名稱得到這個(gè)服務(wù)器對(duì)象的服務(wù)器ID
select object_name(151671588)
--同上相反
--=======學(xué) 云 網(wǎng)-天轟穿-[url]www.ixueyun.co m[/url]===使用其他子句====學(xué) 云網(wǎng)- 天轟 穿-[url]www.ixueyun.com[/url]=========
--首先是 order by 功能 - 排序
select * from studio order by st_name
--多排序條件
select * from studio order by st_name DESC,st_age DESC,st_sex DESC
--有條件,主要是看下條件和子句的位置
select * from studio where cl_id=1 order by st_name
--GROUP BY 子句 功能 - 分組統(tǒng)計(jì)
select cl_id as '班級(jí)編號(hào)',count(*) as '人數(shù)' from studio group by cl_id
--按宿舍統(tǒng)計(jì)年齡平均值
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id
--多分組
select ho_id as '宿舍編號(hào)',cl_id as '班級(jí)編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id,cl_id
--有條件,主要是看下條件和子句的位置
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio where cl_id=1 group by ho_id
--使用 having 子句 功能 - 指定組或者聚合的搜索條件,通常與group by 子句一起使用,完成分組查詢后再進(jìn)步篩選
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id having avg(st_age)>35
--多條件
select ho_id as '宿舍編號(hào)',avg(st_age) as '平均年齡' from studio group by ho_id having avg(st_age)>35 and ho_id>2
--===========聯(lián)合查詢=======學(xué) 云 網(wǎng)-天轟穿-[url]www.ixueyun.com[/url]======
--使用union子句的查詢稱為聯(lián)合查詢,功能:將兩個(gè)以上的查詢結(jié)果集組合為一個(gè)單個(gè)結(jié)果集,該集中包括所有集中的全部行數(shù)據(jù)
--下面我們嘗試將多個(gè)查詢聯(lián)合起來
select * from studio where cl_id=1
union
select * from studio where ho_id=1
union
select * from studio where st_age>=30
--下面我們繼續(xù)利用上面的例題,增加上 All 看下效果
select * from studio where cl_id=1
union all
select * from studio where ho_id=1
union all
select * from studio where st_age>=30
--再繼續(xù)利用,給他加上排序
select * from studio where cl_id=1
union all
select * from studio where ho_id=1
union all
select * from studio where st_age>=30
order by st_id
--===========連接查詢==================
--連接查詢,功能 - 將多個(gè)表中的數(shù)據(jù)查詢出來放在一起
--內(nèi)連接:使用比較運(yùn)算符=><....等進(jìn)行表間某些數(shù)據(jù)庫的比較操作,并列出這些表中與連接條件相匹配的數(shù)據(jù)行
--等值連接,當(dāng)然就是用等號(hào)了,毛病,這也要問
select * from studio inner join class on studio.cl_id = class.cl_id
--指明要查詢的列(江湖上又稱自然連接),并排序
select st_id as '編號(hào)',st_name as '學(xué)生姓名',cl_class as '班級(jí)名稱' from studio inner join class on studio.cl_id = class.cl_id order by st_id
--使用表別名
select st.st_name as '學(xué)生姓名',st.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱' from studio as st inner join class as cl on st.cl_id = cl.cl_id
--不等連接,這個(gè)問題很好笑,既然使用等號(hào)的是等值連接,那么不等值你說是不是應(yīng)該是非等于以外的呢?
--下面我們?cè)龠B接第三個(gè)表,看下是怎么搞滴
select st.st_name as '學(xué)生姓名',st.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱' ,ho.ho_coding as '所在宿舍編號(hào)'
from studio as st inner join class as cl
on st.cl_id = cl.cl_id
inner join hostel as ho
on st.ho_id=ho.ho_id
--我們?cè)俳o他加個(gè)條件看下
--where st.cl_id>2
--再給他個(gè)排序
--order by st.st_id
--外連接:
--與內(nèi)連接不同的是,內(nèi)連接至少要有一個(gè)同屬于兩個(gè)表的行符合連接條件時(shí)才會(huì)返回行,外連接會(huì)返回符合任意條件的行
--他的表有主從之分,他用主表中的每行去匹配從表中的,與內(nèi)連不同的是,他不會(huì)丟棄沒有匹配的行,而是填充null給從結(jié)果集
--左外連接
select st.st_id as '學(xué)生編號(hào)', st.st_name as '學(xué)生姓名',cl.cl_id as '班級(jí)編號(hào)',cl_class as '班級(jí)名稱'
from studio as st left outer join class as cl
on st.cl_id=cl.cl_id
where cl.cl_id>2
--多表
select tka.te_co_id as '課程安排編號(hào)'
,cl.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時(shí)數(shù)'
,te.te_name as '老師姓名'
from te_kc_ap as tka left outer join class as cl
on tka.cl_id=cl.cl_id
left outer join
course as co
on tka.co_id=co.co_id
left outer join
teacher as te
on tka.te_id=te.te_id
--====================右外連結(jié) =======================
select st.st_id as '學(xué)生編號(hào)', st.st_name as '學(xué)生姓名',cl.cl_id as '班級(jí)編號(hào)',cl_class as '班級(jí)名稱'
from studio as st right outer join class as cl
on st.cl_id=cl.cl_id
where cl.cl_id>2
--多表
select tka.te_co_id as '課程安排編號(hào)'
,cl.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時(shí)數(shù)'
,te.te_name as '老師姓名'
from te_kc_ap as tka
right outer join class as cl
on
tka.cl_id=cl.cl_id
right outer join teacher te
on
tka.te_id=te.te_id
right outer join course co
on
tka.co_id=co.co_id
--========完全連接==============
select st.st_id as '學(xué)生編號(hào)', st.st_name as '學(xué)生姓名',cl.cl_id as '班級(jí)編號(hào)',cl_class as '班級(jí)名稱'
from studio as st full outer join class as cl
on st.cl_id=cl.cl_id
order by st.st_id
--多表
select tka.te_co_id as '課程安排編號(hào)'
,cl.cl_id as '班級(jí)編號(hào)',cl.cl_class as '班級(jí)名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時(shí)數(shù)'
,te.te_name as '老師姓名'
from te_kc_ap as tka
full outer join class as cl
on
tka.cl_id=cl.cl_id
full outer join teacher te
on
tka.te_id=te.te_id
full outer join course co
on
tka.co_id=co.co_id
--==========交叉連接================
--該方式在不帶where子句時(shí),返回的是兩個(gè)表中所有數(shù)據(jù)行的笛卡爾積(第一個(gè)表中的行乘以第二個(gè)表中的行)
--用學(xué)生和班級(jí)表做交叉查詢
select st_name,cl_class from studio cross join class
select st_name,cl_class from studio,class
select st_name,cl_class from studio cross join class
--=========自連接===
-----------------先臨時(shí)創(chuàng)建一個(gè)表-------------
create table zone(
id int primary key identity(1,1) not null,
z_zone varchar(30),
z_id int references zone(id))
--大家試下,這里是否可以給個(gè)默認(rèn)值
select * from zone
insert into zone(z_zone) values('北京')
insert into zone(z_zone,z_id) values('北京',4)
insert into zone(z_zone) values('四川')
insert into zone(z_zone,z_id) values('成都',6)
insert into zone(z_zone,z_id) values('綿陽',6)
insert into zone(z_zone) values('江蘇')
insert into zone(z_zone,z_id) values('南京',10)
insert into zone(z_zone,z_id) values('蘇州',10)
insert into zone(z_zone,z_id) values('無錫',10)
insert into zone(z_zone,z_id) values('常州',10)
----------------------------------------------
--看下自連接的一般用處
select a.z_zone,b.z_zone from zone as a inner join zone as b on a.z_id=b.id
--擴(kuò)展應(yīng)用下
select b.z_zone,count(a.z_zone) as '轄區(qū)數(shù)' from zone as a inner join zone as b on a.z_id=b.id group by b.z_zone
--簡單說就是自己連接自己,換言之對(duì)同一個(gè)表進(jìn)行連接操作
select a.st_name,a.st_add,b.st_name,b.st_add from studio as a inner join studio as b on a.st_add=b.st_add
--我們發(fā)現(xiàn)有人等于自己,那么增加一個(gè)條件
select a.st_name,a.st_add,b.st_name,b.st_add from studio as a inner join studio as b on a.st_add=b.st_add and a.st_name!=b.st_name
--====學(xué) 云網(wǎng)-天轟穿-[url]www.ixueyun.com[/url]==子查詢============
--在一個(gè)SQL語句中鑲?cè)肓硪粋€(gè)SQL語句教鑲套查詢,而被鑲?cè)氲倪@個(gè)SQL語句就被江湖人稱子查詢。是處理多表操作的附加方法
--子查詢也稱內(nèi)部查詢,而包含子查詢的Select語句被誠為外部查詢,子查詢自身可以包括一個(gè)或者多個(gè)子查詢,也可以鑲套任意數(shù)量的子查詢
--使用in的子查詢
select * from studio where cl_id in (select cl_id from class where cl_id>2)
--使用 not in
select * from studio where cl_id not in (select cl_id from class where cl_id>2)
--使用比較運(yùn)算符的子查詢 -- any 表示子查詢中任意的值 all 表示子查詢中的每個(gè)值
--使用any
select * from class where cl_id>any(select cl_id from studio where st_age>30)
--使用all
select * from class where cl_id>all(select cl_id from studio where st_age>30)
--============一個(gè)分頁的SQL語句========
select top 3 * from studio
where st_id>all(select top 3 st_id from studio order by st_id)
order by st_id
--使用 exists ,該關(guān)鍵字引入一個(gè)子查詢的時(shí)候基本上是對(duì)數(shù)據(jù)進(jìn)行一次是否存在的測(cè)試
--我們查詢那些人所在的班級(jí)是編號(hào)為 1 的
select * from studio where exists(select cl_id from class where studio.cl_id=class.cl_id and class.cl_id=1)
--使用 not exists
select * from studio where not exists(select * from class where studio.cl_id=class.cl_id and class.cl_id=1) order by st_id
--基于查詢生成新的表
select st_name into class_3 from studio where cl_id=3
--將數(shù)據(jù)批量插入一個(gè)表中
insert into class_3 select st_name from studio where cl_id=4
-----------------------sql 編程--------------
declare @max int;
--申明一個(gè)變量@max
set @max=1;
--為變量@max賦值
while @max<10
--如果@max小于10就進(jìn)入循環(huán)
begin
set @max=@max+1;--每次循環(huán)就給@max加1
print @max;
--打印當(dāng)前@max的值
end
print '終于循環(huán)完了';
相關(guān)文章
mysql 5.7更改數(shù)據(jù)庫的數(shù)據(jù)存儲(chǔ)位置的解決方法
隨著MySQL數(shù)據(jù)庫存儲(chǔ)的數(shù)據(jù)逐漸變大,已經(jīng)將原來的存儲(chǔ)數(shù)據(jù)的空間占滿了,導(dǎo)致mysql已經(jīng)鏈接不上了。所以要給存放的數(shù)據(jù)換個(gè)地方,下面小編給大家分享mysql 5.7更改數(shù)據(jù)庫的數(shù)據(jù)存儲(chǔ)位置的解決方法,一起看看吧2017-04-04Mysql?索引?BTree?與?B+Tree?的區(qū)別(面試)
這篇文章主要介紹了Mysql索引BTree與B+Tree的區(qū)別,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09MySQL定時(shí)任務(wù),清理表數(shù)據(jù)方式
這篇文章主要介紹了MySQL定時(shí)任務(wù),清理表數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11MySQL百萬級(jí)數(shù)據(jù)大分頁查詢優(yōu)化的實(shí)現(xiàn)
在數(shù)據(jù)庫開發(fā)過程中我們經(jīng)常會(huì)使用分頁,但是如果是百萬級(jí)數(shù)據(jù)呢,本文就詳細(xì)的介紹一下MySQL百萬級(jí)數(shù)據(jù)大分頁查詢優(yōu)化的實(shí)現(xiàn),感興趣的可以了解一下2022-01-01MySql創(chuàng)建分區(qū)的方法實(shí)例
mysql分區(qū)相對(duì)于mysql分庫分表便利很多,可以對(duì)現(xiàn)有的mysql大表添加分區(qū),也可以對(duì)已有分區(qū)的表擴(kuò)充分區(qū),下面這篇文章主要給大家介紹了關(guān)于MySql創(chuàng)建分區(qū)的相關(guān)資料,需要的朋友可以參考下2022-04-04CentOS6.9+Mysql5.7.18源碼安裝詳細(xì)教程
CentOS6.9+Mysql5.7.18源碼安裝,以下操作均在root用戶下執(zhí)行。下面通過本教程給大家詳細(xì)介紹CentOS6.9+Mysql5.7.18源碼安裝方法,需要的的朋友參考下吧2017-06-06