MySQL从十进制转换为字符串

时间:2011-02-25 08:07:42

标签: mysql casting

为什么CAST在这个MySQL查询中不起作用?

SELECT  MAX(Signups) AS Max, 
        MIN(Signups) AS Min,
        CAST(ROUND(AVG(Signups),2) AS VARCHAR(3)) AS Avg
FROM
(
    SELECT COUNT(1) AS Signups,
    DATE_FORMAT(JoinDate, "%Y-%m-%d") AS Date
    FROM users
    GROUP BY Date
) z 

为什么我收到此错误?

#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 
'VARCHAR(3)) AS Avg FROM ( ' at line 2

2 个答案:

答案 0 :(得分:4)

这可能是由于MySQL bug #34564: CAST does not accept varchar type

尝试转换为其他类型,如下所示:

CAST(ROUND(AVG(Signups),2) AS CHAR(3)) AS Avg

答案 1 :(得分:0)

VARCHAR更改为CHAR可解决问题