Mysql 仅当给定日期的新记录是新记录时,才计算其数量
仅当记录的日期是记录所有者的min(日期)时,我才尝试计算给定日期的新记录数。以下是我正在尝试运行的查询:Mysql 仅当给定日期的新记录是新记录时,才计算其数量,mysql,sql,mysql-error-1111,Mysql,Sql,Mysql Error 1111,仅当记录的日期是记录所有者的min(日期)时,我才尝试计算给定日期的新记录数。以下是我正在尝试运行的查询: SELECT COUNT(*) FROM user_total_spends WHERE user_id IN (SELECT user_id FROM user_total_spends WHERE MIN(DATE(date_posted)) = '2012-02-07') AND m
SELECT COUNT(*)
FROM user_total_spends
WHERE user_id IN (SELECT user_id
FROM user_total_spends
WHERE MIN(DATE(date_posted)) = '2012-02-07')
AND merchant_location_id = '4f39b201-4a50-40ff-9cdf-cec51506eaf2'
AND date_posted = '2012-02-07';
基本上我想说的是,如果这是该用户/商户遇到的第一天,将其作为该商户的新用户计算
当我运行此命令时,我得到一个
组函数的无效使用
错误。我遗漏了什么?您必须使用HAVING
来过滤一组记录或聚合函数的条件,因此不要使用WHERE min(date(date_posted))=“2012-02-07”
尝试HAVING min(date(date_posted))=“2012-02-07”
尝试以下操作:
SELECT count(*) FROM user_total_spends
WHERE user_id IN (SELECT user_id FROM user_total_spends GROUP BY user_id HAVING min(date(date_posted)) = '2012-02-07')
AND merchant_location_id = '4f39b201-4a50-40ff-9cdf-cec51506eaf2' AND
date_posted = '2012-02-07';
不要使用
WHERE min(date(date_posted))=“2012-02-07”
尝试have min(date(date_posted))=“2012-02-07”
发布解决方案,以便我可以将其标记为答案。那很有效@库珀:注意到了。谢谢现在更正