国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

sql多表內連接查詢(xún)使用的語(yǔ)句是什么

發(fā)布時(shí)間:2021-07-27 11:43 來(lái)源:億速云 閱讀:0 作者:chen 欄目: Mysql 歡迎投稿:712375056

這篇文章主要講解了“sql多表內連接查詢(xún)使用的語(yǔ)句是什么”,文中的講解內容簡(jiǎn)單清晰,易于學(xué)習與理解,下面請大家跟著(zhù)小編的思路慢慢深入,一起來(lái)研究和學(xué)習“sql多表內連接查詢(xún)使用的語(yǔ)句是什么”吧!

在中,可以使用“inner join”語(yǔ)句進(jìn)行多表內連接查詢(xún),只需要使用“select * from 表 inner join 表 on 表1.字段=表2.字段;”語(yǔ)句即可。組合兩個(gè)表中的記錄,返回關(guān)聯(lián)字段相符的記錄。

本教程操作環(huán)境:windows7系統、mysql8版本、Dell G3電腦。

內連接

join 或 inner join

SQL語(yǔ)句:select * from student inner join score on student.Num=score.Stu_id;

此時(shí)的語(yǔ)句就相當于:select * from student,score where student.ID=course.ID;

擴展資料:

外連接

1.左連接  left join 或 left outer join

SQL語(yǔ)句:select * from student left join score on student.Num=score.Stu_id;

2.右連接  right join 或 right outer join

SQL語(yǔ)句:select * from student right join score on student.Num=score.Stu_id;

3.完全外連接  full join 或 full outer join

SQL語(yǔ)句:select * from student full join score on student.Num=score.Stu_id;

通過(guò)上面這三種方法就可以把不同的表連接到一起,變成一張大表,之后的查詢(xún)操作就簡(jiǎn)單一些了。

而對于select * from student,score;則盡量不使用此語(yǔ)句,產(chǎn)生的結果過(guò)于繁瑣。

交叉連接

cross join,沒(méi)有where指定查詢(xún)條件的子句的交叉聯(lián)接將產(chǎn)生兩表的笛卡爾積。

SQL語(yǔ)句:select * from student cross join score;

結構不同的表連接

當兩表為多對多關(guān)系的時(shí)候,我們需要建立一個(gè)中間表student_score,中間表至少要有兩表的主鍵。

SQL語(yǔ)句:select s.Name,C.Cname from student_score as sc left join student as s on s.Sno=sc.Sno left join score as c on c.Cno=sc.Cno

select C_name,grade from student left join score on student.Num=score.Stu_id where name='李五一';

紅色部分即中間表,是集合兩表所有內容的一張總表。

UNION操作符用于合并兩個(gè)或多個(gè)select語(yǔ)句的結果集。

UNION內部的SELECT語(yǔ)句必須擁有相同數量的列,每個(gè)列也必須擁有相似的數據類(lèi)型,每條SELECT語(yǔ)句中的列的順序必須相同。

select Num from student union select Stu_id from score;

union操作符是默認查重的,如果允許重復的值,就可以使用union all 。對于兩張結構相同的表,union也可以把他們合并成一張表:

select * from student1 union select *from student2;

子查詢(xún)

有時(shí)候,查詢(xún)時(shí)需要的條件是另外一個(gè)select語(yǔ)句的結果,就會(huì )使用到子查詢(xún)。

1.帶IN關(guān)鍵字的子查詢(xún)

SQL語(yǔ)句:select * from student where Num IN(select Stu_id from score);

2.帶EXISTS關(guān)鍵字的子查詢(xún)

exists內查詢(xún)返回一個(gè)真價(jià)值,若返回true時(shí),外查詢(xún)進(jìn)行查詢(xún),否則外查詢(xún)不進(jìn)行查詢(xún)。

SQL語(yǔ)句:select * from student where exists(select * from score where C_name='計算機');

3.帶ANY關(guān)鍵字的子查詢(xún)

使用ANY關(guān)鍵字只要有一個(gè)滿(mǎn)足,就通過(guò)該條件來(lái)執行外查詢(xún)。

SQL語(yǔ)句:select sname,(date_format(from_days(now())-to_days(birthday)),'%Y')+0) as '年齡' from student where birthday>ANY(select birthday from student where bumen='計算機系');

4.帶ALL關(guān)鍵字的子查詢(xún)

使用ALL關(guān)鍵字必須滿(mǎn)足所有的內層查詢(xún)語(yǔ)句返回的所有結果,才執行外查詢(xún)

SQL語(yǔ)句:select sname,(date_format(from_days(now())-to_days(birthday)),'%Y')+0) as '年齡' from student where birthday>ALL(select birthday from student where bumen='計算機系');

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

SQL
熟女无套高潮内谢吼叫免费| 午夜男女爽爽影院网站| 国产操逼视频| 国产精品美女久久久久AV超清| av网站的免费观看| 无码午夜人妻一区二区不卡视频|