基于总和的逗号分隔值的连接表

时间:2016-02-15 16:57:53

标签: mysql sql

我有这样的桌面费用

enter image description here

和表benang

enter image description here

当表费用中的id_benang在表benang上具有相同的值时,我可以求和(benang.biaya)吗

所以结果就像这样

id_benang biaya

8 = 4000000

6 = 4000000

       8000000

我的英语不好

1 个答案:

答案 0 :(得分:1)

假设您想要费用信息旁边的benang表中的总价值......

SELECT kain, id_cuci, cost_id_benang, SUM(biaya)
FROM
(
    SELECT b.id_benang, biaya, c.kain, c.id_cuci, c.id_benang as cost_id_benang
    FROM BENANG b
    INNER JOIN 
    COST c
    ON LOCATE( RTRIM(CAST(b.id_benang AS CHAR(10))),c.id_benang) > 0
)AS seperatedData
GROUP BY kain, id_cuci, cost_id_benang

使用此示例架构和种子数据:

CREATE TABLE COST (kain int, id_benang varchar(10), id_cuci int);
CREATE TABLE BENANG (id_benang int, biaya int);

INSERT INTO COST (kain, id_benang, id_cuci)
VALUES(8,'8,6', 3);

INSERT INTO BENANG(id_benang, biaya)
SELECT 5,3000000 UNION ALL
SELECT 6,4000000 UNION ALL
SELECT 7,3000000 UNION ALL
SELECT 8,4000000;