我有一个数据库,记录每个收银员每小时的交易次数。
列名:
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> 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 :(得分: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");