查询如下
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);
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/