這篇文章主要講解了“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í)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站