1. 程式人生 > >程序媛計劃——mysql連接表

程序媛計劃——mysql連接表

mysql連接 sel rom 等值連接 bold mysql right hang 似的

#inner join等值連接/內連接

mysql> select * from info;
+------+-------------+----------+
| name | phone       | address  |
+------+-------------+----------+
| zhao | 13199999999 | Beijing  |
| qian | 1328888888  | Harbin   |
| sun  | 13366666666 | Shanghai |
+------+-------------+----------+
3 rows in set
(0.00 sec)
mysql> select * from score;
+----+------+-------+------------+
| id | name | score | evaluation |
+----+------+-------+------------+
|  1 | zhao | 98.00 | 10         |
|  2 | qian | 90.30 | 10         |
|  3 | sun  | 70.00 | 10         |
|  4 | li   | 80.50 | 10         |
+----+------+-------+------------+
4 rows in set (0.00 sec)

#inner join——按照相同的name值(篩選行)連接兩個表中的某些字段(篩選列)構成表

mysql> select score.id,score.name,info.phone from score inner join info on score.name=info.name;
+----+------+-------------+
| id | name | phone       |
+----+------+-------------+
|  1 | zhao | 13199999999 |
|  2 | qian | 1328888888
| | 3 | sun | 13366666666 | +----+------+-------------+ 3 rows in set (0.00 sec)

#left join——讀取左邊表score的字段的所有值,按照相同的name值連接兩個表中的字段構成表(右邊的表沒有對應記錄的填null)

mysql> select score.id,score.name,info.phone from score left join info on score.name=info.name;
+----+------+-------------+
| id | name | phone       |
+----+------+-------------+
|  1 | zhao | 13199999999 |
|  2 | qian | 1328888888  |
|  3 | sun  | 13366666666 |
|  4 | li   | NULL        |
+----+------+-------------+
4 rows in set (0.00 sec)

#類似的有right join

mysql> select score.id,score.name,info.phone from score right join info on score.name=info.name;
+------+------+-------------+
| id   | name | phone       |
+------+------+-------------+
|    1 | zhao | 13199999999 |
|    2 | qian | 1328888888  |
|    3 | sun  | 13366666666 |
+------+------+-------------+
3 rows in set (0.00 sec)

程序媛計劃——mysql連接表