MySQL(Wordpress) - 更新时的内部联接

时间:2012-11-17 21:40:36

标签: mysql sql wordpress

我试图更新任何“新”字段的后期状态。记录中可以找到重复的post_title' draft'。以下选择查询按预期工作,并显示我要更新的记录 -

select a.* FROM wp_posts AS a INNER JOIN (SELECT Greater1.post_title, Titles.ID, 
Greater1.MinID FROM (SELECT post_title, MIN(ID) AS 'MinID', MAX(ID) AS 'MaxID' FROM 
wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY post_title 
HAVING COUNT(post_title)>1) AS Greater1 LEFT JOIN (SELECT post_title, ID FROM 
wp_posts) AS Titles ON Greater1.post_title = Titles.post_title WHERE ID > MinID) AS 
b ON a.ID = b.ID WHERE a.ID = b.ID

但是以下更新查询会出现语法错误。有什么帮助吗?

update a.wp_posts set a.post_status='draft' FROM wp_posts AS a INNER JOIN (SELECT 
Greater1.post_title, Titles.ID, Greater1.MinID FROM (SELECT post_title, MIN(ID) AS 
'MinID', MAX(ID) AS 'MaxID' FROM wp_posts WHERE post_type = 'post' AND post_status = 
'publish' GROUP BY post_title HAVING COUNT(post_title)>1) AS Greater1 LEFT JOIN 
(SELECT post_title, ID FROM wp_posts) AS Titles ON Greater1.post_title = 
Titles.post_title WHERE ID > MinID) AS b ON a.ID = b.ID WHERE a.ID = b.ID

非常感谢。

2 个答案:

答案 0 :(得分:0)

您使用的语法适用于MSSQL,适用于MySQL

UPDATE wp_posts AS a
        INNER JOIN 
        (
            SELECT Greater1.post_title,
                    Titles.ID,
                    Greater1.MinID
            FROM 
            (
                    SELECT post_title,
                            MIN(ID) AS 'MinID',
                            MAX(ID) AS 'MaxID'
                    FROM wp_posts
                    WHERE post_type = 'post'
                            AND post_status = 'publish'
                    GROUP BY post_title
                    HAVING COUNT(post_title) > 1
            ) AS Greater1
                LEFT JOIN 
                (
                    SELECT post_title,
                            ID
                    FROM wp_posts
                ) AS Titles
                    ON Greater1.post_title = Titles.post_title
            WHERE ID > MinID
        ) AS b ON a.ID = b.ID
SET a.post_status = 'draft'
WHERE a.ID = b.ID

答案 1 :(得分:0)

尝试以下:

       UPDATE wp_posts AS a,
        (SELECT Greater1.post_title,
         Titles.ID, Greater1.MinID 
         FROM 
           (SELECT post_title, 
                   MIN(ID) AS 'MinID', 
                   MAX(ID) AS 'MaxID' 
             FROM wp_posts 
             WHERE post_type = 'post' AND post_status = 'publish' 
             GROUP BY post_title HAVING COUNT(post_title)>1) AS Greater1 
             LEFT JOIN (
                   SELECT post_title, ID 
                   FROM wp_posts) AS Titles 
              ON Greater1.post_title = Titles.post_title 
              WHERE ID > MinID) AS b
         SET a.post_status='draft'
         WHERE a.ID = b.ID;

语法:

         UPDATE TABLE1, TABLE2..
         SET COLUMN1 = ..., COLUMN2 = ...
         WHERE TABALE1.FOREIGN_KEY_ID= TABLE2.ID  //<--joining condition
         AND TABLE1.COLUMN1=... //<--filter condition