Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete data with specific row number (sqlite)

I have a table people (name, address, phone), my table have 2000+ rows. I want to delete 1000 rows. How's about the query?

like image 293
Dennie Avatar asked Oct 15 '09 12:10

Dennie


1 Answers

I assume you want to delete "first 1000 rows" given the unsorted order of the result of "select" query with no sorting arguments and no criteria, in which case you are doing something wrong.

But, as an academic exercise, here is how you'd do it. All rows in an SQLite have rowid field, which you can use to find where those 1000 rows end.

sqlite> create table t(s string);
sqlite> insert into t values('a1');
sqlite> insert into t values('a2');
sqlite> insert into t values('a3');
sqlite> insert into t values('a4');
sqlite> select * from t;
a1
a2
a3
a4
sqlite> delete from t where rowid < (select rowid from t limit 2,1);
sqlite> select * from t;
a3
a4
like image 177
Alex B Avatar answered Sep 22 '22 10:09

Alex B