CAST到DECIMAL在MySQL中 [英] CAST to DECIMAL in MySQL

查看:809
本文介绍了CAST到DECIMAL在MySQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我试图转换表中的行数



SQL代码:

 

code> SELECT CONCAT(Guardian.title,'',
Guardian.forename,'',
Guardian.surname)AS'Guardian Name',
COUNT(*)AS' Number of Activities',
(COUNT(*)* 1.5)AS'Cost'
从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  - 检查
对应于您的MySQL服务器版本的手册,使用正确的语法在第1行的'CAST((COUNT(*)* 1.5)AS DECIMAL(12,2))附近使用

另一个尝试,这个强制类型也不起作用:

  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


在标准SQL中,语法 DECIMAL (M)相当于 DECIMAL(M,0)


所以,你转换为一个带有2位整数和0位十进制数的数字。尝试这样:

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


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?

解决方案

From MySQL docs: Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC:

In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0)

So, you are converting to a number with 2 integer digits and 0 decimal digits. Try this instead:

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

这篇关于CAST到DECIMAL在MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆