2016-05-15 200 views
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) 
+0

即使他們來自不同的日子,你想要加入小時嗎? – Nicarus

+0

不,從同一天 – user3259091

+1

您可能想要考慮使用'EXTRACT(DAY_HOUR FROM fechampresion)'爲您的JOINs,等。 – Nicarus

回答

0

你可以嘗試這樣的

選擇b.second表列中,*。從第一臺,B秒錶,其中a.date列= b.date列

0

我會建議爲表格使用兩個單獨的句柄。類似這樣的:

SELECT DATE_FORMAT(v1.fechaImpresion, "%h") AS fechaImpresion2, COUNT(v1.*) AS total, DATE_FORMAT(v2.fechaImpresion,"%h") as fechaImpresion 
FROM `Venta` v1, 'Venta' v2 where v1.fechaImpresion = v2.fechaImpresion 

然後,您可以根據需要應用各種日期標準。