1

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 ?

Community
  • 1
  • 1
Nicolas Racine
  • 1,031
  • 3
  • 13
  • 35

1 Answers1

4

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
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63