【发布时间】:2014-03-04 15:50:28
【问题描述】:
我有两张表,其中包含客户的年龄和身高。
Table: Ages
+-----------+------------+
|customerId | age |
+-----------+------------+
| 1 | 15 |
| 2 | 24 |
| 3 | 21 |
| 4 | 62 |
| 6 | 57 |
| 7 | 32 |
+-----------+------------+
Table: Heights
+-----------+------------+
|customerId | height |
+-----------+------------+
| 1 | 175 |
| 2 | 182 |
| 4 | 180 |
| 5 | 171 |
| 6 | 165 |
| 7 | 182 |
+-----------+------------+
我需要编写一个SELECT 查询来读取所有年龄和身高。所以像这样......
SELECT Ages.age, Heights.height
FROM Ages INNER JOIN Heights ON Ages.customerId=Heights.customerId;
然而(这里是转折点)由于草率的记录保存,两个表中都缺少记录。 (例如,Ages 中的 customerId 为 5,Heights 中的 customerId 为 3)。
有没有办法编写查询,以便它仍然可以工作,但只要数据丢失就返回零?
即
+-----------+------------+------------+
|customerId | age | height |
+-----------+------------+------------+
| 1 | 15 | 175 |
| 2 | 24 | 182 |
| 3 | 21 | 0 |
| 4 | 62 | 180 |
| 5 | 0 | 171 |
| 6 | 57 | 165 |
| 7 | 32 | 182 |
+-----------+------------+------------+
【问题讨论】: