我想获得今天的用户数量和昨天的用户数量,我想只写一个查询我该怎么做...?
这些是我的查询我只想要一个查询:
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,1 as todayCount
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,0 as yesterdayCount
我的预期结果或仅有2列
todayCount yesterdayCount
2 4
答案 0 :(得分:2)
这应该可以解决问题:
SELECT COUNT(CASE
WHEN visited_date = CURDATE() THEN 1
END) AS todayCount ,
COUNT(CASE
WHEN visited_date = CURDATE() - INTERVAL 1 DAY THEN 1
END) AS yesterdayCount
FROM visitors
WHERE visited_date IN (CURDATE(), CURDATE() - INTERVAL 1 DAY)
GROUP BY visited_date
ORDER by visited_date
答案 1 :(得分:1)
如果你知道当前和之前的日期,那么你可以这样做:
SELECT SUM(visited_date = CURDATE()) as today,
SUM(visited_date = CURDATE() - interval 1 day) as yesterday
FROM visitors
WHERE visited_date >= CURDATE() - interval 1 day;
如果您不知道这两天,那么您可以做类似的事情,获取数据中的最新日期:
SELECT SUM(v.visited_date = m.max_vd) as today,
SUM(v.visited_date < m.max_vd) as yesterday
FROM visitors v CROSS JOIN
(SELECT MAX(v2.visited_date) as max_vd FROM visitors v2) as m
WHERE v.visited_date >= m.max_vd - interval 1 day
答案 2 :(得分:1)
试试这个简单的查询
select visited_date as date, COUNT(*) as count from `visitors`
group by `visited_date` order by `visited_date` asc
它将产生输出
它适用于你。
答案 3 :(得分:-1)
试试这个: $ sqlToday =“选择COUNT(*)FROM menjava WHERE DATE(date_submitted)= CURRENT_DATE()”;
$ sqlY yesterday =“选择COUNT(*)FROM menjava WHERE DATE(dc_created)= CURDATE() - INTERVAL 1 DAY”;