我使用union all來聯合多個查詢,但union all沒有爲這些零值插入空行。我參加了現場一看,並根據這個問題,它應該包括他們:SQL union全部不包含零值
這是我的查詢:
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 1 and SMTH > 0)
union all
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 2 and SMTH > 0)
union all
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 3 and SMTH > 0)
union all
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 4 and SMTH > 0)
結果:
10068
3967
895
四個獨立查詢:
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 1 and SMTH > 0)
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 2 and SMTH > 0)
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 3 and SMTH > 0)
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 4 and SMTH > 0)
結果:
10068
3967
0
895
所以基本上,因爲我認爲,從以前的問題的答案是正確的,我在做什麼錯?
謝謝!
你確定這是MySQL的?使用'[]'作爲標識符是Sql Server的典型代表。 –
這是SQL,但以前回答的問題是MySQL – Eduardo
1.表中的Date值是否唯一? 2.您是如何/在哪裏獲得四個獨立查詢的結果?難道第三個查詢實際上沒有生成行,並且返回結果的應用程序只是默認顯示「0」? –