String ALTER_TBL ="delete from " + MYDATABASE_TABLE +
" where "+KEY_ID+" in (select "+ KEY_ID +" from "+ MYDATABASE_TABLE+" order by _id LIMIT 3);";
- in sqlite that I know about, there is no "top 3" command, you need to add a limit
- notice the spaces when you add lines together:
"delete from" + TABLE + "where" = "delete frommytablewhere"
This approach uses two steps to delete the first N lines.
Find the first N lines:
SELECT id_column FROM table_name ORDER BY id_column LIMIT 3
The result is a list of identifiers that represent the first N (here: 3) lines. Part ORDER BYis important because SQLite does not guarantee any order without this sentence. Without an ORDER BYoperator can delete 3 random lines.
, :
DELETE FROM table_name WHERE id_column IN ( {Result of step 1} )
1 , , N , .
, id_column , , . , , , DELETE FROM table_name WHERE unique_column IN (SELECT unique_column FROM table_name ORDER BY sort_column LIMIT 3). : SQLite ROWID unique_column ( - ).
N last, (DESC):
DELETE FROM table_name WHERE unique_column IN (
SELECT unique_column FROM table_name ORDER BY sort_column DESC LIMIT 3
)
N th M th LIMIT OFFSET. 2 / 3.
SELECT unique_column FROM table_name ORDER BY sort_column LIMIT 3 OFFSET 2
LIMIT (, LIMIT -1 OFFSET 2) , 2, , , SELECT .. WHERE .. IN () SELECT .. WHERE .. NOT IN ()
SQLite , ORDER BY x LIMIT n DELETE . Android , , SQLite :
DELETE FROM table_name ORDER BY sort_column LIMIT 3