具有格式化日期和左连接的GROUP BY

时间:2017-05-15 02:51:12

标签: mysql

我无法正确制作格式化日期组,我认为这可能与我的左连接有关,但我不确定要采取的路径。继承我的查询和结果。为了简单起见,我没有包含额外的列,但请注意,对于选择使用distinct,我不相信是一个选项,因为它会抛弃我的计算。

当我使用它时:

SELECT DATE_FORMAT(posts.post_date, "%Y%m%d") AS 'Date', COUNT(*) AS 'Orders'
FROM wp_posts AS posts
LEFT JOIN wp_postmeta AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND posts.post_status = 'wc-completed'

我明白了:

20170204
20170218
20170301
20170301
20170314
20170401
20170401
20170402
20170403
20170405
20170412
20170426
20170501
20170501
20170501
20170501
20170501
20170504

当我添加Group By时,结果只会下拉到一行:

SELECT DATE_FORMAT(posts.post_date, "%Y%m%d") AS 'Date'
FROM wp_posts AS posts
LEFT JOIN wp_postmeta AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND posts.post_status = 'wc-completed'
GROUP BY 'Date'

我明白了:

20170204

我想要的输出是:

 20170204
 20170218
 20170301
 20170314
 20170401
 20170402
 20170403
 20170405
 20170412
 20170426
 20170501
 20170504

0 个答案:

没有答案