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) 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) 外连接(outer join) 左外连接(left outer join):以左边的表为主表 右外连接(right outer join):以右边的表为主表 以某一个表为主表,进行关联查询,不管能不能关联的上,主表的数据都会保留,关联不上的以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) 左外连接和右外连接是相对的,主要就是以哪个表为主表去进行关联