如何删除 mysql 表中包含另一个查询结果中的字段的行?
该语句如下所示:
DELETE FROM videoswatched vw2
WHERE vw2.userID IN ( SELECT vw.userID
FROM videoswatched vw
JOIN users u ON vw.userID=u.userID
WHERE u.companyID = 1000
GROUP BY userID )
这对我来说看起来不错,并且 SELECT 语句可以独立工作(生成具有单列“userID”的行。
基本上,我想删除“videoswatched”表中的条目,其中 userID 在在加入 users 表后,发现 videowatched 条目的 companyID=1000。
我怎样才能做到这一点而不在我的 sql 语法中出现错误?它说错误就在
vw2 WHERE vw2.userID IN (
SELECT vw.userID FROM videoswatched vw
JOIN users u
第 1 行。
Here's what the statement looks like:
DELETE FROM videoswatched vw2
WHERE vw2.userID IN ( SELECT vw.userID
FROM videoswatched vw
JOIN users u ON vw.userID=u.userID
WHERE u.companyID = 1000
GROUP BY userID )
That looks decent to me, and the SELECT statement works on its own (producing rows with a single column 'userID'.
Basically, I want to delete entries in the 'videoswatched' table where the userID in the videoswatched entry, after joining to the users table, is found to have companyID=1000.
How can I do this without getting the error in my sql syntax? It says the error is near:
vw2 WHERE vw2.userID IN (
SELECT vw.userID FROM videoswatched vw
JOIN users u
and on line 1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
评论中已经回答了:您需要删除表别名,在 MySQL 的 DELETE 语句语法(也没有必要)。
编辑:顺便说一句,试试这个(从记忆中编码,可能是错误的):
It has already been answered in the comments: You need to remove the table alias, it is not allowed in MySQL's DELETE statement syntax (and there is no need for it, either).
Edit: BTW, Try this (coding from memory, might be wrong):
MySQL 手册说
所以,你可以尝试
但是正如评论中提到的,你可以简单地取消别名
The MySQL manual says
So, you could try
But as mentioned in the comments you can simply do away with the alias