我想从数据库中删除某些项目。我有以下查询:
SELECT *
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13
这样可以,并返回2个结果。
现在我想将此SELECT
查询转换为DELETE
查询。但是,以下内容不起作用:
DELETE FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13
MySQL抛出以下错误:
1064 - 您的SQL语法出错;检查手册 对应于您的MySQL服务器 用于正确语法的版本 靠近'WHERE entries.sheetID = sheets.id AND sheets.clientID = 13'at 第1行
我在这里做错了什么?
答案 0 :(得分:10)
MySQL 4及更高版本支持deleting from multiple tables at once,使用以下语法:
DELETE sheets, entries
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13
如果您使用的是低于版本4的MySQL,那么您需要一次从一个表中删除行,并且可以使用此处发布的其他解决方案之一。
答案 1 :(得分:2)
试
DELETE sheets, entries
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13
我一次用2个表格搜索SQL删除,找到了this forum post
答案 2 :(得分:2)
MySQL允许你删除一个连接,但你必须指定哪些列,所以使用你的例子,正确的语法将是
DELETE sheets.*, entries.* FROM sheets, entries WHERE entries.sheetID = sheets.id AND sheets.clientID = 13
答案 3 :(得分:0)
你可以尝试这样的事吗
DELETE FROM sheets
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13
如果要从工作表中删除
DELETE FROM entries
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13
如果来自条目
答案 4 :(得分:0)
我相信你一次只能从一张桌子DELETE
。
DELETE FROM entries
WHERE entries.sheetID IN
(SELECT ID FROM sheets WHERE clientID = 13)
DELETE FROM sheets
WHERE sheets.clientID = 13
答案 5 :(得分:0)
您一次只能从一个表中删除。如果要同时从同一查询中删除两个删除操作,可以执行以下操作:
DELETE from sheets where id in (
SELECT sheets.id
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13);
DELETE from entries where id in (
SELECT entries.id
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13);