我需要计算今天可用记录的几何平均值。当我不包含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。
我错过了一些非常明显的东西吗?
答案 0 :(得分:2)
您需要从日期时间字段upload_time
中提取日期,因为它会将yyyy-mm-dd hh:mm:ss
与yyyy-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()