我正在尝试像这样在 MySQL 中转换为 Decimal:
CAST((COUNT(*) * 1.5) AS DECIMAL(2))
我正在尝试将表中的行数(乘以 1.5)转换为点后两位数的 float 。
SQL 代码:
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
它会产生错误:
#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.
再试一次,这个 Actor 也不行:
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
如何使用mysql将整数转换为十进制?
最佳答案
来自 MySQL 文档:Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC :
In standard SQL, the syntax
DECIMAL(M)
is equivalent toDECIMAL(M,0)
因此,您正在转换为具有 2 个整数位和 0 个小数位的数字。试试这个:
CAST((COUNT(*) * 1.5) AS DECIMAL(12,2))
关于mysql - 在 MySQL 中转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830509/