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 ?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

No yeah, I need to delete all tables rows that match the session table row selected
Oh wow,.. I had it then, But i was using tables names instead,. Well thanks for the fast answer! I have to wait to mark this answer as accepted lol..
An OUTER JOIN in a DELETE clause is a rare event. Maybe there is a reason!
@Strawberry Why are you saying that ? What is the reason ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.