我知道标题可能有点含糊,所以我会试着用一个小例子来解释我想做什么。
我有一个名为 students 的表。我想删除表中多次出现的学生的所有记录。所以 Peter & Aaron 的所有记录都必须删除。
SELECT student, count(student) AS cnt FROM `testtable` GROUP BY `student` HAVING cnt > 1
我还想删除所有数学为 8 的记录。
SELECT id FROM `testtable` WHERE mathematics = 8
但是我如何从这些选择查询变成删除查询呢?甚至可以将两者结合起来吗??
表:学生
id mathematics biology student
-- ----------- ------- -------
0 6 8 Peter
1 6 8 Peter
2 3 9 Aaron
3 8 9 Alicia
4 1 4 Peter
5 7 7 Aaron
6 6 5 Rachel
最佳答案
感谢 Uueerdo,他为我指出了这个方向,我让它开始工作。
DELETE FROM testtable
WHERE mathematics =8
OR student IN (
SELECT stu
FROM (
SELECT student AS stu
FROM `testtable`
GROUP BY `student`
HAVING count( 1 ) >1
)tmp
)
关于mysql - 1 从 2 个选择查询中删除查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30058741/