这里有一个很好的答案:MySQL: Count the distinct rows per day
我需要包含值的日子,但查询
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
WHERE totalCOunt < 1
GROUP BY DATE(timestamp)
给出错误(#1064 - 您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在&#39; WHERE totalCOunt&lt; 1附近使用 限制0,25和#39;在第3行)。我哪里出错了?
答案 0 :(得分:1)
您有一些错误:
Date
应该被转义,因为它是MySQL中的关键字。AS
。所以纠正的是:
SELECT CAST(`timestamp` AS Date) AS `Date`, COUNT(DISTINCT(`ipNum`)) AS totalCOunt
FROM `tableName`
GROUP BY CAST(`timestamp` AS Date)
答案 1 :(得分:1)
在'where子句'错误中抛出未知列'totalCOunt':
请尝试以下查询(用HAVING子句替换WHERE子句):
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
GROUP BY DATE(timestamp)
HAVING totalCOunt<1
答案 2 :(得分:0)
您不能在WHERE子句中使用别名
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
WHERE totalCOunt < 1
GROUP BY DATE(timestamp)
改为使用HAVING:
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
GROUP BY DATE(timestamp)
HAVING totalCOunt =1