How to turn this MySQL SELECT query into a DELETE query?

戏子无情 提交于 2019-12-05 12:41:19

MySQL 4 and up supports deleting from multiple tables at once, using the following syntax:

DELETE sheets, entries
FROM sheets, entries 
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13

If you're using MySQL below version 4, then you need to delete rows from one table at a time, and you can use one of the other solutions posted here.

Matt Ellen

try

DELETE sheets, entries 
FROM sheets, entries
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13

I googled SQL delete from 2 tables at once and found this forum post

MySQL will let you delete over a join, but you must specify which columns, so using your example, the correct syntax would be

DELETE sheets.*, entries.* FROM sheets, entries WHERE entries.sheetID = sheets.id AND sheets.clientID = 13

Can you try something like this

DELETE FROM sheets
FROM sheets, entries 
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13

if you want to delete from sheets and

DELETE FROM entries
FROM sheets, entries 
WHERE entries.sheetID = sheets.id AND sheets.clientID = 13

if its from entries

I believe you can only DELETE from one table at a time.

DELETE FROM entries
WHERE entries.sheetID IN
(SELECT ID FROM sheets WHERE clientID = 13)

DELETE FROM sheets
WHERE sheets.clientID = 13

You can only delete from one table at a time. If you want to drive both deletes from the same query, you can do something like the following:

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!