2014-03-02 95 views
-1

我有一個數據庫,記錄每個收銀員每小時的交易次數。Mysql - 特定日期的特定列

列名:

NAME  00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 DATE 

00是午夜,01是凌晨1點,18是下午6點等。

的平均行應該是這樣的:

John Doe 0 0 0 0 0 0 0 0 0 0 26 24 32 0 0 0 0 0 0 0 0 0 0 0 02/23/2014 
    John Doe 0 0 0 0 0 23 43 25 36 42 0 0 0 0 0 0 0 0 0 0 0 0 0 0 02/22/2014 

我試圖獲得相同的查詢從不同的日子不同的列。

例如,假設我想從2014年2月22日開始的05,06,07,08,09和2014年2月23日開始的10,11,12期間爲John Doe。這將是John Doe整個週末的總小時交易。

假設John Doe本月大部分時間都有排。我怎樣才能得到一條只顯示2014年2月22日的05,06,07,08和09小時以及2014年2月23日的10,11,12小時,而沒有其他日期/小時的線路?

我嘗試這樣做:

SELECT cashier.name AS NAME, 
(SELECT cashier.05 FROM cashier AS t WHERE date = '2014-02-22' AND t.name = cashier.name) as '05', 
(SELECT cashier.06 FROM cashier AS t WHERE date = '2014-02-22' AND t.name = cashier.name) as '06', 
(SELECT cashier.07 FROM cashier AS t WHERE date = '2014-02-22' AND t.name = cashier.name) as '07', 
(SELECT cashier.08 FROM cashier AS t WHERE date = '2014-02-22' AND t.name = cashier.name) as '08', 
(SELECT cashier.09 FROM cashier AS t WHERE date = '2014-02-22' AND t.name = cashier.name) as '09', 
(SELECT cashier.10 FROM cashier AS t WHERE date = '2014-02-23' AND t.name = cashier.name) as '10', 
(SELECT cashier.11 FROM cashier AS t WHERE date = '2014-02-23' AND t.name = cashier.name) as '11', 
(SELECT cashier.12 FROM cashier AS t WHERE date = '2014-02-23' AND t.name = cashier.name) as '12' 
FROM cashier 
WHERE NAME = 'John Doe' 

回答

0
SELECT f.name , f.17, 
(SELECT t.05 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-22') AS '05', 
(SELECT t.06 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-22') AS '06', 
(SELECT t.07 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-22') AS '07', 
(SELECT t.08 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-22') AS '08', 
(SELECT t.09 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-22') AS '09', 
(SELECT t.10 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-23') AS '10', 
(SELECT t.11 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-23') AS '11', 
(SELECT t.12 FROM cashier AS t WHERE f.name = t.name AND t.date = '2014-02-23') AS '12' 
FROM cashier AS f 
WHERE date = '2014-02-22' 
AND f.18 > 0");