SQL語句如何實現(xiàn)超簡單的多表查詢
一、簡單的多表聯(lián)查(inner join,left join,right join)
1、 兩表聯(lián)查
user_table表
department表
1、inner join代表內(nèi)連接,數(shù)據(jù)顯示內(nè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;
查詢結(jié)果如下:
2、left join代表左連接,數(shù)據(jù)顯示內(nèi)容以左邊表為準,意思就是不管右邊表查出來是否有數(shù)據(jù),左邊表的數(shù)據(jù)有的一定會顯示。
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)容以右邊表為準,意思就是不管左邊表查出來是否有數(shù)據(jù),右邊表的數(shù)據(jù)有的一定會顯示。
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語句后面繼續(xù)加上inner join即可,當然這是以內(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ā)器就是當對某個表執(zhí)行某個操作的時候觸發(fā),可以有效防止惡意的sql注入。
到此這篇關(guān)于SQL語句如何實現(xiàn)超簡單多表查詢的文章就介紹到這了,更多相關(guān)SQL語句多表查詢內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。