【问题标题】:Use INNER JOIN for update sql data使用 INNER JOIN 更新 sql 数据
【发布时间】:2021-02-04 06:24:13
【问题描述】:

我想将 wordpress 数据库中的 post_status 从草稿更改为垃圾箱,以获取具有相同 post_title 的重复帖子,并且我使用此 sql 查询

      UPDATE a.post_status SET `post_status` = 'trash'
        FROM wp_posts AS a
         INNER JOIN (
           SELECT post_title, MIN( id ) AS min_id
           FROM wp_posts
           WHERE post_type = 'post'
           AND post_status = 'draft'
           GROUP BY post_title
           HAVING COUNT( * ) > 1
         ) AS b ON b.post_title = a.post_title
        AND b.min_id <> a.id
        AND a.post_type = 'post'
        AND a.post_status = 'draft'

执行查询后显示错误

  Error Code: 1064
    Erreur de syntaxe près de 'FROM wp_posts AS a
     INNER JOIN (
       SELECT post_title, MIN( id ) AS min_id
      ' à la ligne 2

这个查询的正确语法是什么?

【问题讨论】:

    标签: mysql sql database wordpress syntax


    【解决方案1】:

    MySQL中正确的语法不使用FROM

    UPDATE post_status ps INNER JOIN
           (SELECT post_title, MIN( id ) AS min_id
            FROM wp_posts
            WHERE post_type = 'post' AND
                  post_status = 'draft'
            GROUP BY post_title
            HAVING COUNT( * ) > 1
           ) p
           ON p.post_title = ps.post_title AND
              p.min_id <> ps.id AND
              ps.post_type = 'post' AND
              ps.post_status = 'draft'
     SET ps.post_status = 'trash';
    

    请注意,我还将表格别名从无意义的字母更改为更有意义的缩写。

    【讨论】:

    • 谢谢 Gordon 在这个语法中的答案我发现其他错误是 Error Code: 1146 Table 'a.post_status' does not exist
    • @hassanii 立即尝试。
    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 2014-11-30
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2017-01-16
    • 2015-07-29
    相关资源
    最近更新 更多