MySQL语句在Update中使用group_concat

时间:2018-05-15 17:01:44

标签: mysql sql

我想将一些数据从多行复制到一行。当我使用选择所有显示我想要但我无法将其转换为更新查询。 请帮我找问题 选择和更新是:

SELECT CONCAT( P.POST_CONTENT, GROUP_CONCAT(M.meta_value ORDER BY M.meta_id SEPARATOR 'CHAR(10)'))
FROM posts P
INNER JOIN postmeta M ON P.ID=M.post_id
WHERE M.meta_key = 'wpcf-obrazek'
GROUP BY P.ID

    UPDATE P, M
    SET P.post_excerpt = CONCAT( P.POST_CONTENT, GROUP_CONCAT(M.meta_value ORDER BY M.meta_id SEPARATOR 'CHAR(10)'))
    FROM posts P
    INNER JOIN postmeta M ON P.ID=M.post_id
    WHERE M.meta_key = 'wpcf-obrazek'
    GROUP BY P.ID

错误是:

#1064 - Something is wrong in your syntax obok 'FROM posts P
    INNER JOIN postmeta M ON P.ID=M.post_id
    WHERE M.meta_key ' w linii 3

1 个答案:

答案 0 :(得分:1)

如果您将派生表与预处理数据相关联,则更新应该有效,请尝试:

UPDATE P
INNER JOIN (
             SELECT P.ID, CONCAT( P.POST_CONTENT, GROUP_CONCAT(M.meta_value ORDER BY M.meta_id SEPARATOR 'CHAR(10)')) AS data
             FROM posts P
               INNER JOIN postmeta M ON P.ID=M.post_id
             WHERE M.meta_key = 'wpcf-obrazek'
             GROUP BY P.ID
           ) AS excerpt_data ON P.ID = excerpt_data.ID
SET P.post_excerpt = excerpt_data.data;