1

我有一个table_news包含这些字段的表:

_ID = INTEGER PRIMARY KEY AUTOINCREMENT 
ID = TEXT NOT NULL 
CONTENT = TEXT NOT NULL
ZONE TEXT = NOT NULL 
AREA TEXT = NOT NULL
TITLE TEXT = NOT NULL
DATE TEXT = NOT NULL
AUTHOR TEXT = NOT NULL

该表填充如下:

Column    Row 1 Value                   Row 2 Value
------------------------------------------------------
_ID       1                             2
ID        "5"                           "5"
CONTENT   "Discover New York."          "Discover New York."
ZONE      "New York"                    "New York"
AREA      "New York"                    "New York"
TITLE     "Let's discover the world."   "Let's discover the world."
DATE      "2012-07-04"                  "2012-07-04"   
AUTHOR    "Henry Brakman"               "Henry Brakman"

所以我只想做一个 SQL 语句来删除这些重复的行之一。

我试过这个(为便于阅读添加了换行符,命令写为单个字符串):

public SQLiteStatement removeSameContentNews() { 
   return bdd.compileStatement("DELETE FROM table_news "
      + "WHERE _ID NOT IN (SELECT MIN( _ID) "
      + "FROM table_news "
      + "GROUP BY ID, CONTENT, ZONE, AREA, TITLE, DATE, AUTHOR)"); 
}

但这没有删除任何内容..

我终于用 UNIQUE 关键字使用了唯一性,它工作正常:

private static final String CREATE_TABLE_NEWS = 
   "CREATE TABLE " + TABLE_NEWS + " (" + COLUMN_ROWID 
   + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ID 
   + " TEXT NOT NULL UNIQUE, " + COL_CONTENT 
   + " TEXT NOT NULL, " + COL_ZONE + " TEXT NOT NULL, "
   + COL_AREA + " TEXT NOT NULL, " + COL_TITLE 
   + " TEXT NOT NULL, " + COL_DATE + " TEXT NOT NULL, " 
   + COL_AUTHOR + " TEXT NOT NULL);";
4

1 回答 1

2

您可以使用以下查询示例:

DELETE FROM table WHERE _ID NOT IN (
    SELECT MIN( _ID) 
    FROM table 
    GROUP BY ID, CONTENT, ZONE, AREA, TITLE, DATE, AUTHOR
)

编辑

或者,您可以使用:

DELETE FROM table_news WHERE EXISTS
  (SELECT 1 FROM table_news t
    WHERE t.ID=table_news.ID AND 
    t.ID=table_news.CONTENT AND  
    t.ID=table_news.ZONE AND  
    t.ID=table_news.AREA AND  
    t.ID=table_news.TITLE AND  
    t.ID=table_news.DATE AND  
    t.ID=table_news.AUTHOR AND 
    t.rowid > table_news.rowid)
于 2012-07-20T13:53:28.380 回答