我需要运行以下3选择查询:
SELECT profile_gender AS Gender, COUNT(*) AS Users FROM login GROUP BY profile_gender;
SELECT profile_gender AS Gender, COUNT(*) AS Users FROM login WHERE profile_createdate > DATE_SUB(CURDATE(), INTERVAL 1 DAY) GROUP BY profile_gender;
SELECT profile_gender AS Gender, COUNT(*) AS Users FROM login WHERE profile_createdate > DATE_SUB(CURDATE(), INTERVAL 7 DAY) GROUP BY profile_gender;
profile_gender为0或1,并且位于同一列中。是否可以在一个查询中获取此信息?它有点超出了我目前的查询能力。
感谢
答案 0 :(得分:4)
在实际字段列表中进行计数:
SELECT profile_gender AS Gender,
SUM(profile_createdate > (CURDATE() - INTERVAL 1 DAY)) AS Day1,
SUM(profile_createdate > (CURDATE() - INTERVAL 7 DAY)) AS Day7,
COUNT(profile_createdate) AS Total
FROM login
GROUP BY profile_gender
>
比较的布尔结果将通过mysql转换为普通整数0
或1
,然后可以SUM()
编辑。
答案 1 :(得分:3)
您可以使用条件聚合执行此操作:
SELECT profile_gender AS Gender, COUNT(*) AS Users,
sum(profile_createdate > DATE_SUB(CURDATE(), INTERVAL 1 DAY) as cnt_1day,
sum(profile_createdate > DATE_SUB(CURDATE(), INTERVAL 7 DAY) as cnt_7day
FROM login
GROUP BY profile_gender;