选择更多行作为列

时间:2010-05-07 11:52:53

标签: sql mysql database triggers

我有一个MySQL问题

我希望将下一个查询的结果放入var并使用它来更新触发器我试图让名字以逗号分隔

查询返回多行

SELECT t.naam
FROM    trefwoorden t
INNER JOIN organisaties_has_trefwoorden AS o ON (t.id_trefwoorden = o.id_trefwoorden)
WHERE   o.id_organisaties = NEW.id_organisaties;

这是触发器

CREATE TRIGGER updCheck_After AFTER
UPDATE ON organisaties_has_trefwoorden 
FOR EACH row    

    UPDATE organisaties o
    SET    o.trefwoorden_flat = 
    (
        SELECT t.naam
        FROM    trefwoorden t
        INNER JOIN organisaties_has_trefwoorden AS o ON (t.id_trefwoorden = o.id_trefwoorden)
        WHERE   o.id_organisaties = NEW.id_organisaties;        
    )
    WHERE  o.id_organisaties = NEW.id_organisaties  

问题是可以选择更多行作为1列逗号分隔 或者还有其他解决方案吗?

有人建议吗?

1 个答案:

答案 0 :(得分:3)

UPDATE  organisaties o
SET     o.trefwoorden_flat = 
        (
        SELECT  GROUP_CONCAT(t.naam SEPARATOR ',')
        FROM    trefwoorden t
        INNER JOIN
                organisaties_has_trefwoorden AS o
        ON      t.id_trefwoorden = o.id_trefwoorden
        WHERE   o.id_organisaties = NEW.id_organisaties       
        )
WHERE   o.id_organisaties = NEW.id_organisaties