mysql - 使用 INNER JOIN 更新 sql 数据
问题描述
我想将 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 中的正确语法不使用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';
请注意,我还将表格别名从无意义的字母更改为更有意义的缩写。
推荐阅读
- sed - 用另一个文件的内容替换一个文件中的字符串的命令
- git - grepdiff:查找与给定正则表达式不匹配的帅哥?
- google-sheets - 如何从这种 Twilio 格式 2019-08-22 06:12:58 MDT 的文本字符串中提取月份?
- javascript - How to use combineReducers with handleActions
- shell - Counting char in word with different delimiter
- flutter - SQLiteLog( 8646): (1) table tempsettings has no column named user
- php - 组合作为多个块发送的大型 JSON
- reactjs - ReactJS - 通过默认道具和状态生成唯一ID之间的区别
- r - select/filter rows with R: identify values with the same variable or attribute a code to different consecutive cycles
- python - JSON的嵌套序列化