0

我有一个名字是laporan,当前查询是

“从拉波兰选择 *”

然后如果我 print_r 我得到这样的结果数据:

[0] => Array
    (
        [tanggal_laporan] => 2012-11-04
        [total_kas] => Rp 21.000,-
    )

[1] => Array
    (
        [tanggal_laporan] => 2012-11-04
        [total_kas] => Rp 50.500,-
    )

[2] => Array
    (
        [tanggal_laporan] => 2012-11-04
        [total_kas] => Rp 56.000,-
    )

[3] => Array
    (
        [tanggal_laporan] => 2012-11-05
        [total_kas] => Rp 89.000,-
    )

[4] => Array
    (
        [tanggal_laporan] => 2012-11-05
        [total_kas] => Rp 73.000,-
    )

我想求和该字段为 varchar/string 的字段total_kas,我的问题是有办法/可以对字段 varchar/string 求和。然后这个查询 group_by tanggal_laporan,所以我想得到如下数据:

[0] => Array
    (
        [tanggal_laporan] => 2012-11-04
        [total_kas] => 127500
    )

[1] => Array
    (
        [tanggal_laporan] => 2012-11-05
        [total_kas] => 162000
    )

谢谢。

4

1 回答 1

1

这应该有效(假设total_kas始终具有“Rp NUMBER,-”形式):

SELECT tanggal_laporan, SUM(CAST(
    SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(total_kas, '.', ''), ' ', -1), ',', 1)
    AS UNSIGNED INTEGER)) AS num 
FROM laporan 
GROUP BY tanggal_laporan

我做了一些改变来处理整数。这是一个SQLFiddle示例。删除UNSIGNED它不适合您的需求。

于 2013-01-04T08:22:09.680 回答