SQL-Select查询到结果所有12个月,即使数据不存在

时间:2017-10-12 05:02:05

标签: sql

我有桌名' employeecount'以下数据

month , driver, engineer
January , 100,200
March , 300,400
December , 500,600

我想要以下结果

January , 100,200
February , 0,0
March , 300,400
April , 0,0
May , 0,0
June , 0,0
July , 0,0
August , 0,0
September , 0,0
October , 0,0
November , 0,0
December , 500,600

我尝试过创建月份参考表并进行左外连接。但它有效。

请帮助进行查询。

2 个答案:

答案 0 :(得分:2)

使用LEFT JOIN尝试此示例查询。

SELECT ref.month, emp.driver, emp.engineer 
FROM referencetable  ref
LEFT JOIN  employeecount emp
ON emp.month = ref.month;

答案 1 :(得分:0)

实际上,左连接将完全符合您的预期。尝试使用此查询:

select rm.month,e.driver, e.engineer 
from 
refmonth rm left join employeecount e 
on (e.month=rm.month);

在这里,我将参考月份表格设为refmonth,如下所示:

month
January
February
March
April
May
June
July
August
September
October
November
December

因此,如您所述,查询结果将包含NULL而不是0