gpt4 book ai didi

mysql - 在 MySQL 中转换为十进制

转载 作者:IT老高 更新时间:2023-10-28 23:47:25 24 4
gpt4 key购买 nike

我正在尝试像这样在 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 to DECIMAL(M,0)

因此,您正在转换为具有 2 个整数位和 0 个小数位的数字。试试这个:

CAST((COUNT(*) * 1.5) AS DECIMAL(12,2)) 

关于mysql - 在 MySQL 中转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11830509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com