SQL 数据库 连接查询 变量、if else、while
一、連接查詢:通過連接運(yùn)算符可以實(shí)現(xiàn)多個(gè)表查詢。
連接是關(guān)系數(shù)據(jù)庫(kù)模型的主要特點(diǎn),也是它區(qū)別于其它類型數(shù)據(jù)庫(kù)管理系統(tǒng)的一個(gè)標(biāo)志。
常用的兩個(gè)鏈接運(yùn)算符:
1.join?? on(左右連接)
2.union(上下連接) ?注:只有在列的數(shù)據(jù)類型一致時(shí)才能夠連接起來
二、變量
?
SQL語言也跟其他編程語言一樣,擁有變量、分支、循環(huán)等控制語句。
?
在SQL語言里面把變量分為局部變量和全局變量,全局變量又稱系統(tǒng)變量(@@)。
?
?局部變量:
?
使用declare關(guān)鍵字給變量聲明,語法非常簡(jiǎn)單:declare @<變量名> <變量類型>
?
對(duì)變量的賦值可以使用set關(guān)鍵字,使用set關(guān)鍵字時(shí)對(duì)變量的賦值一次只能賦值一個(gè)。
?
我們也可以在查詢語句里面對(duì)這個(gè)變量進(jìn)行賦值。
?
全局變量:又叫做系統(tǒng)變量。
運(yùn)算符:
運(yùn)算符優(yōu)先級(jí)
if。。。else。。
while語句
?
?while if 嵌套
?
--語文成績(jī)最高的學(xué)生信息 select * from stu where scode=(select code from score where yu=(select max(yu) from score)) select *from stu where scode=(select top 1 code from score order by yu desc) --數(shù)學(xué)成績(jī)最低的學(xué)生的任課老師的所有信息 select * from tch where tcode=(select shujiao from stu where scode=(select code from score where shu=(select min(shu) from score))) --查詢匯總成一個(gè)表:各門課分?jǐn)?shù)、學(xué)生姓名、班級(jí)、任課老師的姓名 select stu.sname,banji,score.yu,shu,ying, (select tname from tch where tcode=stu.yujiao) 語文老師, (select tname from tch where tcode=stu.shujiao)數(shù)學(xué)老師, (select tname from tch where tcode=stu.yingjiao)英語老師 from stu join score on stu.scode=score.code print @@version select yu, case yu when 99 then '優(yōu)秀' when 88 then '良好' else '合格' end from score go --查詢每個(gè)班里數(shù)學(xué)最高分 select banji,max(shu) from stu join score on stu.scode=score.code group by banji --查詢語文平均分最高的班級(jí)的老師的信息 select * from tch where tcode= (select top 1 yujiao from stu where banji= (select top 1 banji from stu join score on stu.scode=score.code group by banji order by avg(yu) desc))轉(zhuǎn)載于:https://www.cnblogs.com/baimangguo/p/6085433.html
總結(jié)
以上是生活随笔為你收集整理的SQL 数据库 连接查询 变量、if else、while的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle12c安装出错
- 下一篇: Sass学习笔记