我可以删除所有行但只留下一行吗?

时间:2016-01-04 01:48:24

标签: mysql

我可以删除表格中的所有行但只留下1条记录吗? 像DELETE FROM table_name UNLESS id='5'一样自由地说出来 但id值是一个随机数。 谢谢,

4 个答案:

答案 0 :(得分:2)

韦伯斯特是对的

DELETE FROM table_name WHERE id != 'number';

或者:

DELETE FROM table_name WHERE id NOT LIKE 'number';

答案 1 :(得分:1)

如果您只想留下一个随机行并且您拥有唯一ID,则:

delete t
    from t cross join
         (select id from t order by rand() limit 1
         ) tt
    where t.id <> tt.id;

答案 2 :(得分:1)

for article in articles {

     if let
         title = article["title"] as? String,
         content = article["content"] as? String {

         let entity = NSEntityDescription.insertNewObjectForEntityForName("Article", inManagedObjectContext: DBHelper.context()) as! Article

         entity.title = title
         entity.content = content
     }
}

答案 3 :(得分:0)

试试这个:

DELETE FROM [your_table_name] WHERE id != [your_id];