我有两张 table 。一个是students
,另一个是studentPerformance
students
表包含
id
、name
和 email_id
studentPerformance
表包含 id
、student_id
、marks
我的问题是,如何从students
表中删除所有在studentPerformance
表中没有任何记录的学生?
我用 Google 搜索过,但没有找到任何合适的地方。
谢谢。
最佳答案
左连接:
DELETE s
FROM Students AS s
LEFT JOIN StudentPerformance AS sp
ON sp.student_id = s.id
WHERE sp.student_id IS NULL; -- where not match was found (no sp-row)
或不存在:
DELETE s
FROM Students AS s
WHERE NOT EXISTS (SELECT 1 FROM StudentPerformance AS sp WHERE sp.student_id = s.id);
关于mysql - 删除与依赖表没有任何链接的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48062411/