大家好,
我在mysql数据库上有此查询:
SELECT DAYNAME(added_time) = 'Monday',
COUNT(CASE WHEN MONTH(added_time) = 1 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) mongen,
COUNT(CASE WHEN MONTH(added_time) = 2 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monfeb,
COUNT(CASE WHEN MONTH(added_time) = 3 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monmar,
COUNT(CASE WHEN MONTH(added_time) = 4 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monapr,
COUNT(CASE WHEN MONTH(added_time) = 5 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monmag,
COUNT(CASE WHEN MONTH(added_time) = 6 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) mongiu,
COUNT(CASE WHEN MONTH(added_time) = 7 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monlug,
COUNT(CASE WHEN MONTH(added_time) = 8 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monago,
COUNT(CASE WHEN MONTH(added_time) = 9 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monset,
COUNT(CASE WHEN MONTH(added_time) = 10 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monott,
COUNT(CASE WHEN MONTH(added_time) = 11 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monnov,
COUNT(CASE WHEN MONTH(added_time) = 12 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) mondic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Monday'
UNION
SELECT DAYNAME(added_time) = 'Tuesday',
COUNT(CASE WHEN MONTH(added_time) = 1 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuegen,
COUNT(CASE WHEN MONTH(added_time) = 2 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuefeb,
COUNT(CASE WHEN MONTH(added_time) = 3 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuemar,
COUNT(CASE WHEN MONTH(added_time) = 4 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueapr,
COUNT(CASE WHEN MONTH(added_time) = 5 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuemag,
COUNT(CASE WHEN MONTH(added_time) = 6 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuegiu,
COUNT(CASE WHEN MONTH(added_time) = 7 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuelug,
COUNT(CASE WHEN MONTH(added_time) = 8 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueago,
COUNT(CASE WHEN MONTH(added_time) = 9 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueset,
COUNT(CASE WHEN MONTH(added_time) = 10 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueott,
COUNT(CASE WHEN MONTH(added_time) = 11 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuenov,
COUNT(CASE WHEN MONTH(added_time) = 12 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuedic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Tuesday'
它返回此json:
[{"DAYNAME(added_time) = 'Monday'":"1","mongen":"0","monfeb":"5","monmar":"0","monapr":"2","monmag":"40","mongiu":"63","monlug":"96","monago":"10","monset":"0","monott":"0","monnov":"0","mondic":"0"},{"DAYNAME(added_time) = 'Monday'":"1","mongen":"0","monfeb":"10","monmar":"3","monapr":"2","monmag":"38","mongiu":"59","monlug":"106","monago":"18","monset":"0","monott":"0","monnov":"0","mondic":"0"}]
如何修改查询,使其返回像这样的json?
[{"added_time = 'Monday'","mongen":"0","monfeb":"5","monmar":"0","monapr":"2","monmag":"40","mongiu":"63","monlug":"96","monago":"10","monset":"0","monott":"0","monnov":"0","mondic":"0"},{"added_time = 'Tuesday'","tuegen":"0","tuefeb":"10","tuemar":"3","tueapr":"2","tuemag":"38","tuegiu":"59","tuelug":"106","tueago":"18","tueset":"0","tueott":"0","tuenov":"0","tuedic":"0"}]
预先感谢
Nico
重新思考查询后,确定需要此json:
[{"added_time":"Monday","gen":"0","feb":"5","mar":"0","apr":"2","mag":"40","giu":"63","lug":"96","ago":"10","aet":"0","ott":"0","nov":"0","dic":"0"},{"added_time":"Tuesday","gen":"0","feb":"10","mar":"3","apr":"2","mag":"38","giu":"59","lug":"106","ago":"18","aet":"0","ott":"0","nov":"0","dic":"0"}]
我现在遇到的错误查询是:
SELECT added_time,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 1 THEN 1 ELSE NULL END) gen,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 2 THEN 1 ELSE NULL END) feb,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 3 THEN 1 ELSE NULL END) mar,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 4 THEN 1 ELSE NULL END) apr,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 5 THEN 1 ELSE NULL END) mag,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 6 THEN 1 ELSE NULL END) giu,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 7 THEN 1 ELSE NULL END) lug,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 8 THEN 1 ELSE NULL END) ago,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 9 THEN 1 ELSE NULL END) aet,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 10 THEN 1 ELSE NULL END) ott,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 11 THEN 1 ELSE NULL END) nov,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 12 THEN 1 ELSE NULL END) dic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Monday'
UNION
SELECT added_time,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 1 THEN 1 ELSE NULL END) gen,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 2 THEN 1 ELSE NULL END) feb,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 3 THEN 1 ELSE NULL END) mar,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 4 THEN 1 ELSE NULL END) apr,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 5 THEN 1 ELSE NULL END) mag,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 6 THEN 1 ELSE NULL END) giu,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 7 THEN 1 ELSE NULL END) lug,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 8 THEN 1 ELSE NULL END) ago,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 9 THEN 1 ELSE NULL END) aet,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 10 THEN 1 ELSE NULL END) ott,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 11 THEN 1 ELSE NULL END) nov,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 12 THEN 1 ELSE NULL END) dic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Tuesday'
它返回此错误的json:
[{"added_time":"2018-02-12 09:23:29","gen":"0","feb":"5","mar":"0","apr":"2","mag":"40","giu":"63","lug":"96","ago":"10","aet":"0","ott":"0","nov":"0","dic":"0"},{"added_time":"2018-02-13 09:32:07","gen":"0","feb":"10","mar":"3","apr":"2","mag":"38","giu":"59","lug":"106","ago":"18","aet":"0","ott":"0","nov":"0","dic":"0"}]
如何修改查询,使其成为added_time":"Monday"
而不是added_time":"2018-02-12 09:23:29
?
感谢您的帮助,
Nico
答案 0 :(得分:0)
更改此:
SELECT DAYNAME(added_time) = 'Monday',
收件人:
SELECT DAYNAME(added_time) as added_time,
您的版本中添加了一个没有名称的布尔变量-因此该名称默认为表达式,其值为0/1。您想命名该列,所以使用as
。我会称其为day_of_week
,但您似乎更喜欢added_time
。
我建议您将整个查询更改为:
SELECT DAYNAME(added_time) as added_time,
SUM(MONTH(added_time) = 1) as mongen,
SUM(MONTH(added_time) = 2) as monfeb,
. . .
FROM shipping_details
GROUP BY DAYNAME(added_time)
单个GROUP BY
似乎比UNION ALL
简单得多。将SUM()
与布尔表达式一起使用是MySQL的扩展,它非常方便(在其他编程语言的上下文中也很有意义)。