当Null行在其他数据之前时,访问UNION查询会产生不可读的结果

时间:2014-02-06 19:30:52

标签: ms-access null ms-access-2013 union-all

我有一个Access 2013数据库,其中包含一个名为[carInsurance]的表,如下所示:

enter image description here

现在我想要显示此表中的所有数据以及一个充满NULL值的“空”行。所以我使用UNION查询来完成如下操作:

select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance 
UNION 
select insId, carId, insFromDate from carInsurance 

然而,结果显示如下

enter image description here

当我按如下方式使用UNION ALL时:

select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance 
UNION ALL 
select insId, carId, insFromDate from carInsurance

我得到的所有结果都是正确的,除了我为每个结果得到一个空行。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

我要去预感。我认为这是因为当你给它只有NULL时选择不同的数据类型。打败我为什么UNIONUNION 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