I am trying to cast to Decimal in MySQL like this:
CAST((COUNT(*) * 1.5) AS DECIMAL(2))
I'm trying to convert the number of rows in a table (times 1.5) to a floating point number with two digits after the point.
SQL code:
SELECT CONCAT(Guardian.title, ' ',
Guardian.forename, ' ',
Guardian.surname) AS 'Guardian Name',
COUNT(*) AS 'Number of Activities',
(COUNT(*) * 1.5) AS 'Cost'
FROM Schedule
INNER JOIN Child ON Schedule.child_id = Child.id
INNER JOIN Guardian ON Child.guardian = Guardian.id
GROUP BY Guardian
ORDER BY Guardian.surname, Guardian.forename ASC
It produces an error:
#1064 - 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 'CAST((COUNT(*) * 1.5) AS DECIMAL(12,2))' at line 1.
Another try, this cast also doesn't work:
SELECT CONCAT(Guardian.title, ' ',
Guardian.forename, ' ',
Guardian.surname) AS 'Guardian Name',
COUNT(*) AS 'Number of Activities',
CAST((COUNT(*) * 1.5) AS DECIMAL(8,2)) AS 'Cost'
FROM Schedule
INNER JOIN Child ON Schedule.child_id = Child.id
INNER JOIN Guardian ON Child.guardian = Guardian.id
GROUP BY Guardian
ORDER BY Guardian.surname, Guardian.forename ASC
How do I use mysql to cast from integer to decimal?
GROUP BY Guardian
???Guardian
is a table, right? SHouldn't it beGROUP BY Guardian.id
? – WeedyCONCAT(...something... AS ...something..., ...more stuff here...)
is what's causing the syntax error. CompareSELECT CONCAT('foo', 'bar', CAST(8 / 5 AS DECIMAL(12,2)));
andSELECT CONCAT('foo' AS foo, 'bar' AS bar, CAST(8 / 5 AS DECIMAL(12,2)) AS cost);
. – Thermogenesis