开发者

MySQL casting from decimal to string

Why doesn't the CAST work in this MySQL query?

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 

Why am I getting this 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 
'VARCHAR(3)) AS Avg FROM ( ' at line 2


This may be due to MySQL bug #34564: CAST does not accept varchar type.

Try casting to a different type, like this:

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


Changing VARCHAR to CHAR solves the problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜