我有一个Access 2013数据库,其中包含一个名为[carInsurance]的表,如下所示:
现在我想要显示此表中的所有数据以及一个充满NULL值的“空”行。所以我使用UNION查询来完成如下操作:
select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance
UNION
select insId, carId, insFromDate from carInsurance
然而,结果显示如下
当我按如下方式使用UNION ALL时:
select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance
UNION ALL
select insId, carId, insFromDate from carInsurance
我得到的所有结果都是正确的,除了我为每个结果得到一个空行。我该如何解决这个问题?
答案 0 :(得分:3)
我要去预感。我认为这是因为当你给它只有NULL时选择不同的数据类型。打败我为什么UNION
与UNION ALL
会有所不同。
试试这个:
Select
cast (NULL as int) as insId,
cast (NULL as int) as carId,
cast (NULL as Date) as insFromDate
from carInsurance
UNION
Select insId, carId, insFromDate from carInsurance
扩展同样的思路,我建议改变SELECT的顺序:
Select insId, carId, insFromDate from carInsurance
UNION
Select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance