gpt4 book ai didi

mysql - 删除在单独表中找不到 userID 的行

转载 作者:行者123 更新时间:2023-11-29 05:06:58 26 4
gpt4 key购买 nike

如果我运行以下 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 JOINIS 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com