Answer the question
In order to leave comments, you need to log in
How to combine DELETE and LEFT JOIN?
There is a table of users (user) and a table of students (students). If a student takes acad. vacation, then information about him in students is deleted, but as user he remains. The task is to find and remove just such students, they have student_id - NULL.
Tell me, where is the error?
DELETE FROM user LEFT JOIN students ON user.id = students.student_id WHERE student_id IS NULL
Answer the question
In order to leave comments, you need to log in
Those. the task to delete all users who are not students?
If yes, then you can try this:
DELETE user
FROM user u
WHERE NOT EXISTS(SELECT 1
FROM students s
where s.student_id = u.id)
multi-table delete has a slightly different syntax , you must explicitly specify the tables from which records should be deleted
DELETE `user` FROM `user` LEFT JOIN `students` ON `user`.id = `students`.student_id WHERE student_id IS NULL
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question