MYSQL几何平均值计算不适用于CURRENT_DATE()

时间:2014-08-21 02:05:32

标签: mysql

我需要计算今天可用记录的几何平均值。当我不包含CURRENT_DATE()时,下面的查询工作正常,但这对于获取今天的记录至关重要。

SELECT id,EXP( SUM( LOG( atmosphere ) ) / COUNT( atmosphere ) ) AS atmosphere,
       EXP( SUM( LOG( gender ) ) / COUNT( gender) ) AS gender,
       EXP( SUM( LOG( wait ) ) / COUNT( wait ) ) AS wait
FROM ratings
WHERE location_id = $id AND upload_time= CURRENT_DATE()

upload_time在MYSQL表中设置为DATETIME。

我错过了一些非常明显的东西吗?

2 个答案:

答案 0 :(得分:2)

您需要从日期时间字段upload_time中提取日期,因为它会将yyyy-mm-dd hh:mm:ssyyyy-mm-dd

进行比较

使用mysql的DATE()函数 - http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

你最终会在你的where语句

中结束这一点
DATE(upload_time) = CURRENT_DATE()

答案 1 :(得分:1)

试试这个:

SELECT id,EXP( SUM( LOG( atmosphere ) ) / COUNT( atmosphere ) ) AS atmosphere,EXP( SUM( LOG( gender ) ) / COUNT( gender) ) AS gender,EXP( SUM( LOG( wait ) ) / COUNT( wait ) ) AS wait FROM ratings WHERE location_id = $id AND DATE(upload_time) = CURRENT_DATE()