日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

SQL 数据库 连接查询 变量、if else、while

發布時間:2025/3/14 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL 数据库 连接查询 变量、if else、while 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、連接查詢:通過連接運算符可以實現多個表查詢。

連接是關系數據庫模型的主要特點,也是它區別于其它類型數據庫管理系統的一個標志。

常用的兩個鏈接運算符

1.join?? on(左右連接)

2.union(上下連接) ?注:只有在列的數據類型一致時才能夠連接起來

二、變量

?

SQL語言也跟其他編程語言一樣,擁有變量、分支、循環等控制語句。

?

SQL語言里面把變量分為局部變量全局變量,全局變量又稱系統變量(@@)。

?

?局部變量:

?

使用declare關鍵字給變量聲明,語法非常簡單:declare @<變量名> <變量類型>

?

對變量的賦值可以使用set關鍵字,使用set關鍵字時對變量的賦值一次只能賦值一個。

?

我們也可以在查詢語句里面對這個變量進行賦值。

?

全局變量:又叫做系統變量。

運算符:

運算符優先級

if。。。else。。

while語句

?

?while if 嵌套

?

--語文成績最高的學生信息 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) --數學成績最低的學生的任課老師的所有信息 select * from tch where tcode=(select shujiao from stu where scode=(select code from score where shu=(select min(shu) from score))) --查詢匯總成一個表:各門課分數、學生姓名、班級、任課老師的姓名 select stu.sname,banji,score.yu,shu,ying, (select tname from tch where tcode=stu.yujiao) 語文老師, (select tname from tch where tcode=stu.shujiao)數學老師, (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 '優秀' when 88 then '良好' else '合格' end from score go --查詢每個班里數學最高分 select banji,max(shu) from stu join score on stu.scode=score.code group by banji --查詢語文平均分最高的班級的老師的信息 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))

轉載于:https://www.cnblogs.com/baimangguo/p/6085433.html

總結

以上是生活随笔為你收集整理的SQL 数据库 连接查询 变量、if else、while的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。