Mysql的连接

Mysql内连接=等值连接就相当于求两个表的相同处
表1

select * from table1;
+----+---------+--------+------------+
| id | title   | author | date       |
+----+---------+--------+------------+
|  1 | java    | z      | 2018-03-26 |
|  2 | php     | a      | 2018-03-26 |
|  3 | phython | s      | 2018-03-25 |
+----+---------+--------+------------+
3 rows in set

表2

select * from tcount_tbl;
+--------+-------+
| author | count |
+--------+-------+
| z      |    20 |
| a      |    20 |
| b      |     0 |
| g      |     0 |
| j      |     1 |
| s      |     1 |
+--------+-------+
6 rows in set

内连接语法

select * from table1 inner join table2 on 调教

实例

select * from table1 a inner join tcount_tbl b on a.author=b.author;
+----+---------+--------+------------+--------+-------+
| id | title   | author | date       | author | count |
+----+---------+--------+------------+--------+-------+
|  1 | java    | z      | 2018-03-26 | z      |    20 |
|  2 | php     | a      | 2018-03-26 | a      |    20 |
|  3 | phython | s      | 2018-03-25 | s      |     1 |
+----+---------+--------+------------+--------+-------+

left join on以左表为主表

select a.id ,a.author,b.count from table1 a left join tcount_tbl b on a.author=b.author;
+----+--------+-------+
| id | author | count |
+----+--------+-------+
|  1 | z      |    20 |
|  2 | a      |    20 |
|  3 | s      |     1 |
+----+--------+-------+

right join on以右表为主表

 select a.id ,a.author ,b.count from table1 a right join tcount_tbl b on a.author =b.author;
+------+--------+-------+
| id   | author | count |
+------+--------+-------+
|    1 | z      |    20 |
|    2 | a      |    20 |
|    3 | s      |     1 |
| NULL | NULL   |     0 |
| NULL | NULL   |     0 |
| NULL | NULL   |     1 |
+------+--------+-------+

你可能感兴趣的:(Mysql的连接)