mysql內連接和外連接的區別:內連接會取出連接表中匹配到的數據,匹配不到的不保留;而外連接會取出連接表中匹配到的數據,匹配不到的也會保留,其值為NULL。
本教學操作環境:windows7系統、mysql8版本、Dell G3電腦。
#範例表
##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)內連接(inner join)
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)
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)外連接(outer join)
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影片教學】
以上是mysql內連線和外連線有什麼差別的詳細內容。更多資訊請關注PHP中文網其他相關文章!