常用的SQL例句 資料庫開發所需知識

來源:互聯網
上載者:User

--查看學生表的全部資料
select * from studio
--插入一個新的學生資訊
insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黃蘭淇",0,36,'南充','13943943334')
--查看class全部資料
select * from class
--向class表增加兩條條資料
insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新電實訓班','GXA-ncs-001','2008-03-11','都是很優秀的朋友')
insert into class(cl_class,cl_coding,cl_o_time) values('阿壩師專實訓班','GXA-ABSZ-001','2008-03-11')
--更新一條的資料 條件的重要性
update class set cl_remark='真的是不錯' where cl_id=5
--刪除一條資料 條件的重要性
delete from class where cl_id=7
--修改欄位標題
select cl_id as '班級主鍵',cl_class as '班級名稱' from class
select 名字=st_name from studio
--使用文字串
select '名字是:',st_name from studio
--=============條件稍微複雜點的查增刪改==============
--主要涉及到 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('千星一班','百傑二班')
--=================使用數學運算子======================
--主要涉及到 + = * \
--查詢Java相關課程分別要上多少周 按照每周5天,每天6節課來計算
select '結果'=co_num/5/6 from course where co_name in ('Java基礎','Java項目入門')
--==================使用彙總函數 ========================
--涉及到COUNT SUM AVG MAX MIN
--查詢課時數小於50的課程一共有多少門
select count(*) from course where co_num<50
--查詢所有課程一共多少課時
select sum(co_num) from course
--計算全部課時費,假設每節課50塊錢
select sum(co_num)*50 from course
--查詢課時最少的課程
select min(co_num) from course
--查詢課時最多的課程
select max(co_num) from course
--查詢平均每門課多少課時
select avg(co_num) from course
--=================使用數學函數=============================
--包括求絕對值函數ABS函數、求圓周率函數PI()、求正玄值SIN()函數、求指數函數EXP()等。
--查詢每門課的正弦值
select sin(co_num) from course
--查詢每門課的絕對值
select abs(co_num) from course
--查詢每門課課時數 乘以 圓周率 ,具體有什麼用我也不知道,反正這好像絕對是8.5杆子都打不到的
select pi()*co_num from course
--查詢每門課的指數
select exp(co_num) from course
--隨機返回5個隨機產生的數(返回的是0~1之間的隨機float值)
declare @i tinyint
set @i=1
while @i<=5
begin
select rand(@i) as '隨機產生的數' , @i as '當前值'
set @i=@i+1
end
--返回數字運算式並四捨五入為指定的長度或精度 - ROUND
select round(345.456,-1) as '參數為-1'
, round(345.456,-2,1) as '參數為-2'
, round(345.456,0) as '參數為0'
, round(345.456,1) as '參數為1'
, round(345.456,2) as '參數為2'
--================使用日期函數======================
--DAY()、MONTH()、YEAR()——返回指定日期的天數、月數、年數;
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)——在日期上增加給定日期類型的數量;
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 - 小時
hh
--datepart - 分鐘
mi、n
--datepart - 秒
ss、s
--datepart - 毫秒
ms
--DATEDIFF(datepart,date1,date2)——擷取兩個日期之間給定的日期類型的數量差(整個函數結果是date2-date1);
select datediff(mm,cl_s_time,cl_o_time) as '共持續月' from class
--datepart(datepart,date)——在給定日期基礎上返回指定日期類型的值(整數);
--其實這個等同於DAY、MONTH、和 YEAR 函數
select datepart(dd,cl_s_time) as '日期' from class
--GETDATE()——返回當前日期和時間。我們在設計資料庫的時候,通常也可能把他作為預設值
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
--=============使用字串函數=====================
--字串連結運算子
select '結果顯示' = '班級名稱是:' + cl_class + ',班級編號是:' + cl_coding from class
--使用SUBSTRING函數截取字串
select substring(cl_class,1,4) from class
--從字串的左邊開始返回3個字元
select left(cl_class,3) from class
--同理,返回右邊的
select right(cl_class,3) from class
--傳回值的字元數
select len(cl_class) from class
--替換
select replace(cl_class,'實訓','強化') from class
--==============使用系統函數====================
select host_id()
--返回工作站標識號
select host_name()
--返回工作站所啟動並執行電腦名稱
select db_id()
select db_name()
select object_id('Stu_course_ADD')
--通過名稱得到這個伺服器對象的伺服器ID
select object_name(151671588)
--同上相反
--=======學 雲 網-天轟穿-[url]www.ixueyun.co m[/url]===使用其他子句====學 雲網- 天轟 穿-[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 子句 功能 - 分組統計
select cl_id as '班級編號',count(*) as '人數' from studio group by cl_id
--按宿舍統計年齡平均值
select ho_id as '宿舍編號',avg(st_age) as '平均年齡' from studio group by ho_id
--多分組
select ho_id as '宿舍編號',cl_id as '班級編號',avg(st_age) as '平均年齡' from studio group by ho_id,cl_id
--有條件,主要是看下條件和子句的位置
select ho_id as '宿舍編號',avg(st_age) as '平均年齡' from studio where cl_id=1 group by ho_id
--使用 having 子句 功能 - 指定組或者彙總的搜尋條件,通常與group by 子句一起使用,完成分組查詢後再進步篩選
select ho_id as '宿舍編號',avg(st_age) as '平均年齡' from studio group by ho_id having avg(st_age)>35
--多條件
select ho_id as '宿舍編號',avg(st_age) as '平均年齡' from studio group by ho_id having avg(st_age)>35 and ho_id>2
--===========聯集查詢=======學 雲 網-天轟穿-[url]www.ixueyun.com[/url]======
--使用union子句的查詢稱為聯集查詢,功能:將兩個以上的查詢結果集組合為一個單個結果集,該集中包括所有集中的全部行資料
--下面我們嘗試將多個查詢聯合起來
select * from studio where cl_id=1
union
select * from studio where ho_id=1
union
select * from studio where st_age>=30
--下面我們繼續利用上面的例題,增加上 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
--再繼續利用,給他加上排序
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
--===========串連查詢==================
--串連查詢,功能 - 將多個表中的資料查詢出來放在一起
--內串連:使用比較子=><....等進行表間某些資料庫的比較操作,並列出這些表中與串連條件相匹配的資料行
--等值串連,當然就是用等號了,毛病,這也要問
select * from studio inner join class on studio.cl_id = class.cl_id
--指明要查詢的列(江湖上又稱自然串連),並排序
select st_id as '編號',st_name as '學生姓名',cl_class as '班級名稱' from studio inner join class on studio.cl_id = class.cl_id order by st_id
--使用表別名
select st.st_name as '學生姓名',st.cl_id as '班級編號',cl.cl_class as '班級名稱' from studio as st inner join class as cl on st.cl_id = cl.cl_id
--不等串連,這個問題很好笑,既然使用等號的是等值串連,那麼不等值你說是不是應該是非等於以外的呢?
--下面我們再串連第三個表,看下是怎麼搞滴
select st.st_name as '學生姓名',st.cl_id as '班級編號',cl.cl_class as '班級名稱' ,ho.ho_coding as '所在宿舍編號'
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
--我們再給他加個條件看下
--where st.cl_id>2
--再給他個排序
--order by st.st_id
--外串連:
--與內串連不同的是,內串連至少要有一個同屬於兩個表的行符合串連條件時才會返回行,外串連會返回符合任意條件的行
--他的表有主從之分,他用主表中的每行去匹配從表中的,與內連不同的是,他不會丟棄沒有匹配的行,而是填充null給從結果集
--左外串連
select st.st_id as '學生編號', st.st_name as '學生姓名',cl.cl_id as '班級編號',cl_class as '班級名稱'
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 '課程安排編號'
,cl.cl_id as '班級編號',cl.cl_class as '班級名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時數'
,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
--====================右外連結 =======================
select st.st_id as '學生編號', st.st_name as '學生姓名',cl.cl_id as '班級編號',cl_class as '班級名稱'
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 '課程安排編號'
,cl.cl_id as '班級編號',cl.cl_class as '班級名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時數'
,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 '學生編號', st.st_name as '學生姓名',cl.cl_id as '班級編號',cl_class as '班級名稱'
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 '課程安排編號'
,cl.cl_id as '班級編號',cl.cl_class as '班級名稱'
,co.co_id as '課程ID',co.co_name as '課程名稱',co.co_num as '課時數'
,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子句時,返回的是兩個表中所有資料行的笛卡爾積(第一個表中的行乘以第二個表中的行)
--用學生和班級表做交叉查詢
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
--=========自串連===
-----------------先臨時建立一個表-------------
create table zone(
id int primary key identity(1,1) not null,
z_zone varchar(30),
z_id int references zone(id))
--大家試下,這裡是否可以給個預設值
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
--擴充應用下
select b.z_zone,count(a.z_zone) as '轄區數' from zone as a inner join zone as b on a.z_id=b.id group by b.z_zone
--簡單說就是自己串連自己,換言之對同一個表進行串連操作
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
--我們發現有人等於自己,那麼增加一個條件
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
--====學 雲網-天轟穿-[url]www.ixueyun.com[/url]==子查詢============
--在一個SQL語句中鑲入另一個SQL語句教鑲套查詢,而被鑲入的這個SQL語句就被江湖人稱子查詢。是處理多表操作的附加方法
--子查詢也稱內部查詢,而包含子查詢的Select語句被誠為外部查詢,子查詢自身可以包括一個或者多個子查詢,也可以鑲套任意數量的子查詢
--使用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)
--使用比較子的子查詢 -- any 表示子查詢中任意的值 all 表示子查詢中的每個值
--使用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)
--============一個分頁的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 ,該關鍵字引入一個子查詢的時候基本上是對資料進行一次是否存在的測試
--我們查詢那些人所在的班級是編號為 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
--將資料批量插入一個表中
insert into class_3 select st_name from studio where cl_id=4
-----------------------sql 編程--------------
declare @max int;
--申明一個變數@max
set @max=1;
--為變數@max賦值
while @max<10
--如果@max小於10就進入迴圈
begin
set @max=@max+1;--每次迴圈就給@max加1
print @max;
--列印當前@max的值
end
print '終於迴圈完了';

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.