在下面的查詢中,我正在執行表日曆和第二個表的連接,我需要爲查詢指定一些參數。mysql權限加入和條件
的表的連接是在領域calendar.datefield執行和store_product.created
的問題是,我被id_store = 3過濾和在2012年7月20日store_product表記錄另一行有id_store = 4,因此查詢不會帶來結果。有沒有什麼辦法來顯示這個結果爲NULL,迫使mysql忽略這種情況?
結果:(沒有顯示包含日期2012-07-20的行,因爲它具有其他id_store)
DATE price
2012-07-17 NULL
2012-07-18 700.00
2012-07-19 NULL
2012-07-21 NULL
2012-07-22 NULL
2012-07-23 NULL
我想顯示
DATE price
2012-07-17 NULL
2012-07-18 700.00
2012-07-19 NULL
->>> 2012-07-20 NULL
2012-07-21 NULL
2012-07-22 NULL
2012-07-23 NULL
查詢:
set @id_store = 3;
set @id_product = 11;
SELECT
calendar.datefield as DATE,
t1.price
FROM
store_product t1
RIGHT JOIN
calendar ON (DATE(t1.created) = calendar.datefield)
WHERE
(calendar.datefield BETWEEN ('2012-07-17') and ('2012-07-23'))
AND (t1.id_store = @id_store OR t1.id_store is NULL)
AND (t1.id_product = @id_product OR t1.id_product is NULL)
AND (t1.created = (select
max(f2.created)
from
store_product f2
where
f2.id_store = t1.id_store
and f2.id_product = t1.id_product
and DATE(t1.created) = DATE(f2.created))
OR t1.created is NULL)
GROUP BY DATE , t1.id_store , t1.id_product
't1'上的所有條件都應該在'ON'子句中。 – 2012-07-30 12:22:45
Tks尼古拉..我把id_store和id_product在ON子句和工作.. – adrianogf 2012-07-30 12:40:21