在连接后显示表的所有行

时间:2017-08-10 14:16:49

标签: mysql sql database join

我想在连接表B中匹配列的元素后显示表A的所有行

表A

+-----------+----------+------------+
| FirstName | LastName |    Date    |
+-----------+----------+------------+
| Tia       | Carrera  | 1975-09-18 |
| Nikki     | Taylor   | 1972-03-04 |
| Yamila    | Diaz     | 1970-03-05 |
+-----------+----------+------------+

表B

+-----------+------------+
|    code   | Date       |
+-----------+------------+
| 1234      | 1975-09-18 |
| 56789     | 1972-03-04 |
| 54856     | 1968-07-14 |
+-----------+------------+

我在网上找到的所有内容都是

+-----------+----------+------------+-----------+
| FirstName | LastName |    Date    |    code   |
+-----------+----------+------------+-----------+
| Tia       | Carrera  | 1975-09-18 | 1234      |
| Nikki     | Taylor   | 1972-03-04 | 56789     |
+-----------+----------+------------+-----------+

但这是我想要的结果

+-----------+----------+------------+-----------+
| FirstName | LastName |    Date    |    code   |
+-----------+----------+------------+-----------+
| Tia       | Carrera  | 1975-09-18 | 1234      |
| Nikki     | Taylor   | 1972-03-04 | 56789     |
| Yamila    | Diaz     | 1970-03-05 |           |
+-----------+----------+------------+-----------+

1 个答案:

答案 0 :(得分:3)

您需要left join

select a.*, b.code
from a left join
     b
     on a.date = b.date;