A
A
Asikov Artur2019-11-21 17:36:14
SQL
Asikov Artur, 2019-11-21 17:36:14

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

2 answer(s)
R
Ruslan., 2019-11-21
@Artur937

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)

L
Lazy @BojackHorseman, 2019-11-21
SQL

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 question

Ask a Question

731 491 924 answers to any question