我想只选择当天的记录。所以我每天都能看到有多少访客(" bezoekers")。
这是我使用的代码:
Select FROM_UnixTime((sensordata1.time), '%Y-%c-%d-%H') "Datum (Descending)",
COUNT(DISTINCT address) "Bezoekers"
FROM sensordata1
ORDER BY 1
FROM_UnixTime((sensordata1.time), '%Y-%c-%d-%H') DESC
我尝试了什么:
Select FROM_UnixTime((sensordata1.time), '%Y-%c-%d-%H') "Datum (Descending)",
COUNT(DISTINCT address) "Bezoekers"
FROM sensordata1
WHERE 1, DATE('Datum') = DATE(CURDATE()
ORDER BY 1
FROM_UnixTime((sensordata1.time), '%Y-%c-%d-%H') DESC
我想要的是什么:
Datum | Bezoekers
2017-4-20-15 | 31
2017-4-20-14 | 34
2017-4-20-13 | 20
2017-4-20-12 | 26
我不想要例如那里的2017-04-21。 如果我第二天回来,那就需要从那天开始,所以你不能约会。
答案 0 :(得分:0)
您错过了GROUP BY
。如果您想要每个FROM_UnixTime(sensordata1.time, '%Y-%c-%d-%H')
一个结果行,则按其分组。
Select
FROM_UnixTime(sensordata1.time, '%Y-%c-%d-%H') AS "Datum (Descending)",
COUNT(DISTINCT address) AS "Bezoekers"
FROM sensordata1
GROUP BY FROM_UnixTime(sensordata1.time, '%Y-%c-%d-%H')
ORDER BY FROM_UnixTime(sensordata1.time, '%Y-%c-%d-%H') DESC;
仅适用于当天(添加了WHERE
条款):
Select
FROM_UnixTime(sensordata1.time, '%Y-%c-%d-%H') AS "Datum (Descending)",
COUNT(DISTINCT address) AS "Bezoekers"
FROM sensordata1
WHERE FROM_UnixTime(sensordata1.time, '%Y-%m-%d') = curdate()
GROUP BY FROM_UnixTime(sensordata1.time, '%Y-%c-%d-%H')
ORDER BY FROM_UnixTime(sensordata1.time, '%Y-%c-%d-%H') DESC;
答案 1 :(得分:0)
我想指出您可以使用别名来简化查询。日期逻辑有点不清楚,但你似乎想要昨天的约会。您可以使用where
子句执行此操作:
Select from_unixtime(sd.time, '%Y-%c-%d-%H') as Datum,
count(distinct sd.address) as Bezoekers
from sensordata1 sd
where sd.time >= unixtime_timestamp(curdate() - interval 1 day) and
sd.time < unixtime_timestamp(curdate())
group by datum
order by datum desc