SQL根据同一个表中的查询删除行?

时间:2014-10-18 13:55:46

标签: mysql sql

鉴于此查询:

SELECT cur.`id`
FROM `current` AS cur
LEFT OUTER JOIN (
    SELECT MAX(EndDay_id.id) as id, EndDay_id.server_id
    FROM current as EndDay_id
    GROUP BY server_id, gameday
    ) AS EndDay
ON cur.id = EndDay.id
WHERE EndDay.id IS null

如何删除表格中的行' current'其id字段是否在上一个查询的结果集中?

2 个答案:

答案 0 :(得分:0)

delete current
where id in (SELECT cur.`id`
FROM `current` AS cur
LEFT OUTER JOIN (
SELECT MAX(EndDay_id.id) as id, EndDay_id.server_id
FROM current as EndDay_id
GROUP BY server_id, gameday
) AS EndDay
ON cur.id = EndDay.id
WHERE EndDay.id IS null)

答案 1 :(得分:0)

试试这个

DELETE cur FROM `current` AS cur
LEFT OUTER JOIN (
    SELECT MAX(EndDay_id.id) as id, EndDay_id.server_id
    FROM current as EndDay_id
    GROUP BY server_id, gameday
    ) AS EndDay
ON cur.id = EndDay.id
WHERE EndDay.id IS null

我测试了它,这应该有效:http://sqlfiddle.com/#!2/a47c81