简体   繁体   中英

Delete rows from multiple tables with a left join

I searched a lot online and I can't figure out how do to do my simple task.

I want to delete all those row that im selecting with this following query.

`SELECT * FROM `session` s
LEFT JOIN `pages` p ON (s.Id = p.VisitorSessionId)
LEFT JOIN `product_views`ps ON (s.Id = ps.VisitorSessionId)
WHERE s.`JSEnabled` = 0

And I tried to delete those row by using

DELETE FROM `session` s
LEFT JOIN `pages` p ON (s.Id = p.VisitorSessionId)
LEFT JOIN `product_views`ps ON (s.Id = ps.VisitorSessionId)
WHERE s.`JSEnabled` = 0

I tried to do something similar to this post answer Deleting rows with MySQL LEFT JOIN But its not working.

How can i make this query without having to do a loop in my application ?

If you need to delete only from one table then it could be done as

DELETE s FROM `session` s
LEFT JOIN `pages` p ON (s.Id = p.VisitorSessionId)
LEFT JOIN `product_views`ps ON (s.Id = ps.VisitorSessionId)
WHERE s.`JSEnabled` = 0

For deleting from multiple tables with join it would be as

DELETE s,p,ps FROM `session` s
LEFT JOIN `pages` p ON (s.Id = p.VisitorSessionId)
LEFT JOIN `product_views`ps ON (s.Id = ps.VisitorSessionId)
WHERE s.`JSEnabled` = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM