Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You have an error in your SQL syntax...near 'float) / CAST(rating_count AS float)) as average_rating from document'

Following is the query

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);

and error I am getting

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

I can't figure out why is it giving syntax error.

like image 846
shreyas Avatar asked Dec 02 '25 20:12

shreyas


1 Answers

Try like this:

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 MySQL documentation says:

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]
like image 176
Rahul Tripathi Avatar answered Dec 04 '25 16:12

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!