SQL語(yǔ)句如何實(shí)現(xiàn)超簡(jiǎn)單的多表查詢(xún)
一、簡(jiǎn)單的多表聯(lián)查(inner join,left join,right join)
1、 兩表聯(lián)查

user_table表

department表

1、inner join代表內(nèi)連接,數(shù)據(jù)顯示內(nèi)容以外鍵為準(zhǔn),意思就是外鍵沒(méi)有的,數(shù)據(jù)就不顯示。
select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname from user_table inner join department on user_table.departmentid=department.did;
查詢(xún)結(jié)果如下:

2、left join代表左連接,數(shù)據(jù)顯示內(nèi)容以左邊表為準(zhǔn),意思就是不管右邊表查出來(lái)是否有數(shù)據(jù),左邊表的數(shù)據(jù)有的一定會(huì)顯示。
select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname from user_table left join department on user_table.departmentid=department.did;

3、right join代表右連接,數(shù)據(jù)顯示內(nèi)容以右邊表為準(zhǔn),意思就是不管左邊表查出來(lái)是否有數(shù)據(jù),右邊表的數(shù)據(jù)有的一定會(huì)顯示。
select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname from user_table right join department on user_table.departmentid=department.did;

2、三表聯(lián)查
只需要在sql語(yǔ)句后面繼續(xù)加上inner join即可,當(dāng)然這是以?xún)?nèi)連接為主。如下:
work表

department表后面添加work表的主鍵作為關(guān)聯(lián)的外鍵

select user_table.id,user_table.username,user_table.sex,user_table.phone,user_table.address,department.dname,work.worktext from user_table inner join department on user_table.departmentid=department.did inner join work on work.workid=department.workid;

二、觸發(fā)器
觸發(fā)器就是當(dāng)對(duì)某個(gè)表執(zhí)行某個(gè)操作的時(shí)候觸發(fā),可以有效防止惡意的sql注入。
到此這篇關(guān)于SQL語(yǔ)句如何實(shí)現(xiàn)超簡(jiǎn)單多表查詢(xún)的文章就介紹到這了,更多相關(guān)SQL語(yǔ)句多表查詢(xún)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信