2012-11-17 48 views
1

我試圖更新任何'新'記錄的字段post-status可以找到重複的post_title'草稿'。以下選擇查詢按預期工作並顯示我想要更新的記錄 -MySQL(WordPress的) - 內部加入時更新

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 

非常感謝。

+0

也許這是圍繞MinID和MaxID的那些引號,你應該擺脫 – Horen

+0

你從mysql得到的錯誤是什麼? – Horen

回答

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 
+0

謝謝,就是我想要的。乾杯!!! – user1640119

+0

@ user1640119歡迎您:D –

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