I have following tables
question
-------------------
id | name
quiz_results
------------------
id|question_id|is_correct
Accidentally i deleted some question from the backend Note: I have used laravel framework and used model to maintain dependency. I havent used any relationship on database level. Now i need to remove the orphan records from quiz_results(ie i need to delete those records whose question was deleted accidentally)
I tried below
delete from quiz_results where id IN (SELECT qr.id
FROM `quiz_results` qr
LEFT JOIN questions q ON ( q.id = qr.question_id )
WHERE q.id IS NULL )
it is giving following error
Earlier I also tried without subquery like below
delete from
FROM `quiz_results` qr
LEFT JOIN questions q ON ( q.id = qr.question_id )
WHERE q.id IS NULL
which throw the syntax error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM quiz_results
qr LEFT JOIN questions q ON ( q.id = qr.question_id ) WH' at line 2
Try the follwing query:
DELETE FROM quiz_results WHERE question_id NOT IN (SELECT id FROM question)
Try this
delete from quiz_results r
where not exists
(
select 1
from question q
where q.id=r.question_id
)
delete from FROM `quiz_results`
-->
delete `quiz_results` FROM `quiz_results`
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.