3
請有人可以幫我。我需要加入這兩個查詢和小時分組。查詢結果可能會有很多天。按加入日期排序查詢
結果:Query1
SELECT DATE_FORMAT(Venta.fechaImpresion, "%h") AS fechaImpresion2, COUNT(*) AS total
FROM `Venta`
WHERE Venta.fechaImpresion LIKE "2016-05-02 %"
GROUP BY HOUR(Venta.fechaImpresion) , DAY(Venta.fechaImpresion)
LIMIT 0 , 30
結果:Query2
SELECT DATE_FORMAT(Venta.fechaImpresion,"%h") as fechaImpresion FROM `Venta`
where Venta.fechaImpresion >= "2016-05-02" and Venta.fechaImpresion <= "2016-05-09"
GROUP BY hour(Venta.fechaImpresion)
,如果我嘗試在一個查詢中加入的結果是零。
SELECT DATE_FORMAT(Venta.fechaImpresion, "%h") AS fecha, dia1.total AS total1
FROM `Venta`
LEFT JOIN (
SELECT DATE_FORMAT(Venta.fechaImpresion, "%h") AS fechaImpresion
FROM `Venta`
WHERE Venta.fechaImpresion >= "2016-05-02"
AND Venta.fechaImpresion <= "2016-05-09"
GROUP BY HOUR(Venta.fechaImpresion)
)fecha ON fecha.fechaImpresion = Venta.fechaImpresion
LEFT JOIN (
SELECT DATE_FORMAT(Venta.fechaImpresion, "%h") AS fechaImpresion2, COUNT(*) AS total
FROM `Venta`
WHERE Venta.fechaImpresion LIKE "2016-05-02 %"
GROUP BY HOUR(Venta.fechaImpresion) , DAY(Venta.fechaImpresion)
)dia1 ON (dia1.fechaImpresion2 = fecha.fechaImpresion)
WHERE Venta.fechaImpresion >= "2016-05-02"
AND Venta.fechaImpresion <= "2016-05-09"
GROUP BY HOUR(Venta.fechaImpresion)
即使他們來自不同的日子,你想要加入小時嗎? – Nicarus
不,從同一天 – user3259091
您可能想要考慮使用'EXTRACT(DAY_HOUR FROM fechampresion)'爲您的JOINs,等。 – Nicarus