2014-07-04 46 views
0

請給予幫助,(SQL查詢)設置相同列中沒有數據的值與另一行比較

我嘗試使用來自PHP頁面的odbc連接對Ms.Access進行查詢。

我有這個TABEL(表1

class quantity date 
1 30 01/04/2014 
2 23 01/04/2014 
3 23 01/04/2014 
4 14 01/04/2014 
5 5 01/04/2014 
1 41 01/05/2014 
2 38 01/05/2014 
3 36 01/05/2014 
4 28 01/05/2014 
5 25 01/05/2014 
6 1 01/05/2014 

請給查詢的幫助得到這個輸出:

class quantity date 
1 30 01/04/2014 
2 23 01/04/2014 
3 23 01/04/2014 
4 14 01/04/2014 
5 5 01/04/2014 
6 0 0 
1 41 01/05/2014 
2 38 01/05/2014 
3 36 01/05/2014 
4 28 01/05/2014 
5 25 01/05/2014 
6 1 01/05/2014 

在輸出將顯示0的數量爲6個班究竟是誰在01/04/2014沒有第6班的記錄。

+0

這實際上是一個表或它是一個查詢嗎?你怎麼知道有6,而不是7或8班? – Brad

+0

以上是Microsoft Access中的一個表格, 通常我在我的數據庫中有1到6個班級,而在第4個月(4月)沒有班級6的記錄。 在輸出中,我需要每個類中的所有類拋出值min 0值。 –

回答

0

你可以用union all來做到這一點。但更普遍的解決方案是這樣的:

select c.class, d.date, nz(t1.quantity, 0) 
from ((select distinct class from table1) as c cross join 
     (select distinct date from table1) as d 
    ) left join 
    table1 as t1 
    on t1.class = c.class and t1.date = d.date 
+0

謝謝,
但我stil得到此錯誤:odbc_exec():SQL錯誤:[Microsoft] [ODBC Microsoft Access驅動程序] JOIN操作中的語法錯誤 –

相關問題