将MySQL十进制转换为可接受的格式,以便能够序列化它

时间:2012-05-31 19:14:54

标签: mysql sql

  

可能重复:
  Python JSON serialize a Decimal object

我有以下SQL:

SELECT 
    concat(UNIX_TIMESTAMP(date), '000') as datetime, 
    SUM(royalty_price) as sales 
FROM 
    sales_raw 
GROUP BY 
    datetime

结果如下所示:

datetime        sales
1337151600000   1045.71 (decimal)
1337238000000   478.04
1337324400000   300.96
1337410800000   289.02

由此我得到以下错误:

Exception Type: TypeError at /ajax/graph/
Exception Value: Decimal('1045.71') is not JSON serializable

如何在mysql中将小数转换为可接受的格式以便能够序列化这些数据?

1 个答案:

答案 0 :(得分:4)

如果必须在MySQL中执行,只需CAST到字符串:

SELECT 
    CONCAT(UNIX_TIMESTAMP(date), '000') AS datetime, 
    CAST(SUM(royalty_price) AS CHAR)    AS sales
FROM 
    sales_raw 
GROUP BY 
    datetime