我目前有多个查询,我想收集到一个,他们是相同的,只是日期范围正在改变:
查询1:
SELECT COUNT(simp_posts.ID)
FROM simp_posts
INNER JOIN simp_postmeta
ON ( simp_posts.ID = simp_postmeta.post_id )
WHERE ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value = '0' AND simp_posts.post_status = 'wc-active'))
OR ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value > '2018-08-01 23:33:51' AND simp_posts.post_status = 'wc-active'))
OR ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value > '2018-08-01 23:33:51' AND simp_posts.post_status = 'wc-pending-cancel'))
问题2:
SELECT COUNT(simp_posts.ID)
FROM simp_posts
INNER JOIN simp_postmeta
ON ( simp_posts.ID = simp_postmeta.post_id )
WHERE ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value = '0' AND simp_posts.post_status = 'wc-active'))
OR ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value > '2018-09-01 23:33:51' AND simp_posts.post_status = 'wc-active'))
OR ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value > '2018-09-01 23:33:51' AND simp_posts.post_status = 'wc-pending-cancel'))
问题3:
SELECT COUNT(simp_posts.ID)
FROM simp_posts
INNER JOIN simp_postmeta
ON ( simp_posts.ID = simp_postmeta.post_id )
WHERE ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value = '0' AND simp_posts.post_status = 'wc-active'))
OR ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value > '2018-10-01 23:33:51' AND simp_posts.post_status = 'wc-active'))
OR ( ( simp_postmeta.meta_key = '_schedule_end' AND simp_postmeta.meta_value > '2018-10-01 23:33:51' AND simp_posts.post_status = 'wc-pending-cancel'))
我想这些查询被合并成一个,并有一个结果是3行与每个计数的结果在一个。例如,我会得到:182 183 194
谢谢你的帮助
2条答案
按热度按时间qlckcl4x1#
在mysql中,可以使用
SUM()
带布尔值。你的逻辑很复杂。我认为可以归结为:
wvt8vs2t2#
有共同的条件
where
并在日期范围内有条件地进行聚合。注意@tim在评论中提到的内容。如果计数必须相互排斥,则包括meta_value
在case
表情。