当我运行此查询时,我在phpmydamin上出现此错误消息:'having clause'中的未知列'timestamp'
我的专栏名称为timestamp
SELECT DISTINCT (
hash
) AS total
FROM behaviour
HAVING total =1 and date(timestamp) = curdate()
如何获取今天的哈希值?
答案 0 :(得分:3)
使用where
。括号不适用于select distinct
(distinct
不是函数)。我怀疑你打算:
SELECT COUNT(DISTINCT hash) AS total
FROM behaviour
WHERE date(timestamp) = curdate();
最好在不使用列上的函数的情况下编写WHERE
子句:
SELECT COUNT(DISTINCT hash) AS total
FROM behaviour
WHERE timestamp >= curdate() AND timestamp < date_add(curdate, interval 1 day);
虽然更复杂,但它允许数据库引擎在behaviour(timestamp)
上使用索引(或者更好,在behaviour(timestamp, hash)
上。
编辑:
如果您希望仅显示一次的hash
,则一个方法是子查询:
select count(*)
from (select hash
from behaviour
where timestamp >= curdate() AND timestamp < date_add(curdate, interval 1 day)
group by hash
having count(*) = 1
);
答案 1 :(得分:0)
计算仅存在一次的哈希值:
select count(*)
from
(
select hash
from behavior
where date(timestamp) = curdate()
group by hash
having count(*) = 1
) dt
内部选择(派生表)将返回仅存在一次的哈希值。外部选择将计算这些行。