mysql - 你的 SQL 语法有错误...在 'float)/CAST(rating_count AS float)) as average_rating from document' 附近

标签 mysql sql mysql-error-1064

查询如下

select id, IF(rating_count = 0, null, CAST(rating_sum AS float) / CAST(rating_count AS float)) as average_rating 
from document d left join document_aggregate_rating using (id) where id in (123);

我得到的错误

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'float) / CAST(rating_count AS float)) as average_rating from document' at line 1

我不明白为什么会出现语法错误。

最佳答案

像这样尝试:

select id, IF(rating_count = 0, null, 
CAST(rating_sum AS DECIMAL(10,6)) / CAST(rating_count AS DECIMAL(10,6))) as average_rating 
from document d left join document_aggregate_rating 
using (id) where id in (123);

MySQL documentation说:

The type for the result can be one of the following values:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

关于mysql - 你的 SQL 语法有错误...在 'float)/CAST(rating_count AS float)) as average_rating from document' 附近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291001/

相关文章:

java - 将数据从mysql转换到hbase

php - 升级到 4.3.1 最新版本后 Wordpress 自定义查询 'no database selected'

带子选择的 Mysql 查询花费太长时间..

mysql - 如何在 MySQL 中提取部分 Base64 编码的字符串?

php - MariaDB - 发票编号 - 每年返回0

sql - MSSQL - 授予除一个表之外的所有表的选择权限

php - 从多个表中选择行的最佳方法?

mysql - 错误 1064 (42000) && 错误 1005 (HY000) : Can't create table Contact_Info_Tbl (errno: 150)

mysql - 当表结构在 MySQL 中不匹配时使用 INSERT INTO SELECT

mysql - C3p0 和 MySql CommunicationsException : Communications link failure