[英]deleting orphan record - mysql
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.我不小心从后端删除了一些问题 注意:我使用了 laravel 框架并使用模型来维护依赖关系。 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)
现在我需要从 quiz_results 中删除孤立记录(即我需要删除那些问题被意外删除的记录)
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; #1064 - 你的 SQL 语法有错误; 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检查与您的 MySQL 服务器版本相对应的手册,在第 2 行的“FROM
quiz_results
qr LEFT JOIN questions q ON (q.id = qr.question_id) WH”附近使用正确的语法
Try the follwing query:尝试以下查询:
DELETE FROM quiz_results WHERE question_id NOT IN (SELECT id FROM question) 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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.