2

重複するpost_titleが見つかった「new」レコードのフィールドpost-statusを「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

どうもありがとう。

4

2 に答える 2

0

使用している構文はforですMSSQL、これはforです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
于 2012-11-17T21:42:54.807 に答える
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
于 2012-11-17T21:47:25.750 に答える