如果我运行以下 SQL 语句:
SELECT * FROM `user-data` t1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL
它为我提供了 users
表中没有匹配行的所有行(因为用户已被删除)。但我不能简单地将其转换为 DELETE
语句:
DELETE FROM `user-data` t1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL
当我收到以下错误时:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL' at line 1
如何删除 user-data
中表 users
中没有现有用户的所有行?
最佳答案
不要使用 LEFT OUTER JOIN
和 IS NULL
尝试 WHERE NOT EXISTS
。像这样:
DELETE t1
FROM user-data t1
WHERE NOT EXISTS
(
SELECT id FROM FROM users WHERE user-data.userID = users.id)
)
关于mysql - 删除在单独表中找不到 userID 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46090764/