您好,登錄后才能下訂單哦!
這篇文章主要介紹“mysql內連接和外連接有哪些區別”,在日常操作中,相信很多人在mysql內連接和外連接有哪些區別問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mysql內連接和外連接有哪些區別”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
mysql內連接和外連接的區別:內連接會取出連接表中匹配到的數據,匹配不到的不保留;而外連接會取出連接表中匹配到的數據,匹配不到的也會保留,其值為NULL。
本教程操作環境:windows7系統、mysql8版本、Dell G3電腦。
內連接(inner join):取出連接表中匹配到的數據,匹配不到的不保留
外連接(outer join):取出連接表中匹配到的數據,匹配不到的也會保留,其值為NULL
示例表
users表
mysql> select * from users; +----+-------+ | id | name | +----+-------+ | 1 | john | | 2 | May | | 3 | Lucy | | 4 | Jack | | 5 | James | +----+-------+ 5 rows in set (0.00 sec)
topics表
mysql> select * from topics; +----+---------------------------------------+---------+ | id | title | user_id | +----+---------------------------------------+---------+ | 1 | Hello world | 1 | | 2 | PHP is the best language in the world | 2 | | 3 | Laravel artist | 6 | +----+---------------------------------------+---------+ 3 rows in set (0.00 sec)
示例
mysql> select * from users as u inner join topics as t on u.id=t.user_id; +----+------+----+---------------------------------------+---------+ | id | name | id | title | user_id | +----+------+----+---------------------------------------+---------+ | 1 | john | 1 | Hello world | 1 | | 2 | May | 2 | PHP is the best language in the world | 2 | +----+------+----+---------------------------------------+---------+ 2 rows in set (0.00 sec)
inner可以省略,as是給表起別名,也可以省略
mysql> select * from users u join topics t on u.id=t.user_id; +----+------+----+---------------------------------------+---------+ | id | name | id | title | user_id | +----+------+----+---------------------------------------+---------+ | 1 | john | 1 | Hello world | 1 | | 2 | May | 2 | PHP is the best language in the world | 2 | +----+------+----+---------------------------------------+---------+ 2 rows in set (0.00 sec)
以上兩句等價于
mysql> select * from users,topics where users.id=topics.user_id; +----+------+----+---------------------------------------+---------+ | id | name | id | title | user_id | +----+------+----+---------------------------------------+---------+ | 1 | john | 1 | Hello world | 1 | | 2 | May | 2 | PHP is the best language in the world | 2 | +----+------+----+---------------------------------------+---------+ 2 rows in set (0.00 sec)
左外連接(left outer join):以左邊的表為主表
右外連接(right outer join):以右邊的表為主表
以某一個表為主表,進行關聯查詢,不管能不能關聯的上,主表的數據都會保留,關聯不上的以NULL顯示
通俗解釋就是:先拿出主表的所有數據,然后到關聯的那張表去找有沒有符合關聯條件的數據,如果有,正常顯示,如果沒有,顯示為NULL
示例
mysql> select * from users as u left join topics as t on u.id=t.user_id; +----+-------+------+---------------------------------------+---------+ | id | name | id | title | user_id | +----+-------+------+---------------------------------------+---------+ | 1 | john | 1 | Hello world | 1 | | 2 | May | 2 | PHP is the best language in the world | 2 | | 3 | Lucy | NULL | NULL | NULL | | 4 | Jack | NULL | NULL | NULL | | 5 | James | NULL | NULL | NULL | +----+-------+------+---------------------------------------+---------+ 5 rows in set (0.00 sec)
等價于以下,只是字段的位置不一樣
mysql> select * from topics as t right join users as u on u.id=t.user_id; +------+---------------------------------------+---------+----+-------+ | id | title | user_id | id | name | +------+---------------------------------------+---------+----+-------+ | 1 | Hello world | 1 | 1 | john | | 2 | PHP is the best language in the world | 2 | 2 | May | | NULL | NULL | NULL | 3 | Lucy | | NULL | NULL | NULL | 4 | Jack | | NULL | NULL | NULL | 5 | James | +------+---------------------------------------+---------+----+-------+ 5 rows in set (0.00 sec)
左外連接和右外連接是相對的,主要就是以哪個表為主表去進行關聯
到此,關于“mysql內連接和外連接有哪些區別”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。