0

我的数据库中有一个表(最终),我想删除所有重复的行,除了
那些具有 NULL 值的行;
我试过上面的:

 DELETE FROM Final 
  WHERE event_id NOT IN (SELECT MIN(event_id) AS MinID 
                           FROM (SELECT * FROM Final) as x 
                          GROUP BY event_name
                            AND event_name IS NOT NULL)

但没有用。

编辑

我的桌子看起来像:

编号 | 珊瑚
1 | 空
2 | 1
3 | 1 <- 删除
4 | 2
5 | NULL <- 保持原样
6 | 1 <-删除

4

2 回答 2

1

只需将AND event_name IS NOT NULL部分移动到主子查询:

DELETE FROM Final 
  WHERE event_id NOT IN (SELECT MIN(event_id) AS MinID 
                           FROM (SELECT * FROM Final) as x 
                          GROUP BY event_name)
    AND event_name IS NOT NULL
于 2013-03-29T19:11:04.063 回答
1

加入表格怎么样?

DELETE  a
FROM    Final a
        LEFT JOIN
        (
            SELECT  Event_name, MIN(Event_ID) MIN_ID
            FROM    Final
            WHERE   Event_name IS NOT NULL
            GROUP   BY Event_name
        ) b ON a.Event_name = b.Event_name AND
                a.Event_ID = b.MIN_ID
WHERE   a.Event_name IS NOT NULL AND 
        b.Event_name IS NULL
于 2013-03-27T14:20:58.297 回答