I have created a recently viewed items table. In that I would like to delete the first 20 items how do I go about deleting them?
First in the sense here the first 20 items that was created.
I know the query:
DELETE FROM table ORDER BY id DESC LIMIT 20, 18446744073709551615; (this is random number)
How do I write this in Android format by using DB.Dlete
SQLiteDatabase db = this.getWritableDatabase();
//DELETE FROM table ORDER BY id DESC LIMIT 20, 18446744073709551615;
db.delete(TABLE_RECENT_VIEWED, "ORDER BY " + KEY_RECENTVIEWED_ID_LOCAL + " DESC LIMIT 10, 18446744073709551615", null);
db.close();
I keep getting E/SQLiteLog: (1) near "ORDER": syntax error
Can somebody guide me delete only first 20 records? (for example i have 40 records of which 1 is first record and 40 is the last record.)
Not sure where I am going wrong?
Thanks!
What you want: delete the FIRST 20 ROWS
First you will have to get that rows by:
SELECT Id
FROM table
ORDER By Id ASC
LIMIT 20
You should be using ascending (ASC) in the order by, if you really want the first 20 rows. If you use descending (DESC), that would be the last 20 rows.
After that, you can now delete the records by:
DELETE FROM table
WHERE Id IN (
SELECT Id
FROM table
ORDER By Id ASC
LIMIT 10
)
Related
I have ids in my table, ids start from 1 to 20, I want a query, to find the first and last records in a given table but I want the result by some condition.
For example: if I have the record
1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
This is the island part of the classic gaps and islands problem (With the gaps part being finding the missing values in between each island). If you search for that term, you'll find a ton of material about how to calculate them.
One approach (Requires Sqlite 3.25 or newer for window function support):
sqlite> CREATE TABLE ex(id INTEGER PRIMARY KEY);
sqlite> INSERT INTO ex VALUES (1),(2),(3),(4),(5),(9),(10),(11),(12),(13),(19),(20);
sqlite> WITH cte AS (SELECT id, id - row_number() OVER (ORDER BY id) AS grp FROM ex)
...> SELECT min(id) AS rangestart, max(id) AS rangeend FROM cte GROUP BY grp;
rangestart rangeend
---------- ----------
1 5
9 13
19 20
SQL Query to find first record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> ASC LIMIT 1
SQL Query to find last record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> DESC LIMIT 1
For example: if I have the record 1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
If you need result like you have mentioned, then you can set LIMIT in your query to get how many records you can have in that query.
QUERY:
SELECT * FROM <table_name> LIMIT <any_number>
I have a database from which I would like to take the last 3 records. For example if I had the lines 1,2,3,4,5,6, ... 10,11,12,13,14, I would like 12,13,14 no matter the order (12,13,14 for me is equal to 14,13,12).
I tried to follow another question at this link Android SQLite Query - Getting latest 10 records
but what I get is just showing the first 3 rows of the database.
This is my query
String query2 ="select * from (select * from USERS order by ID ASC limit 3)";
In any case you can sort by rowid descending and get 3 rows:
select * from USERS order by rowid desc limit 3
If you want to sort by a specific column:
select * from USERS order by columnname desc limit 3
I have some Entity to store my data into ROOM. How to store only 10 last row to my db using room. For now I'n using #Query("SELECT * FROM Entity LIMIT 10") but it's dont looks right
We you want only first 10 or less records in your database then you have to set the id as auto increment and try to delete all those records where the ids doesn't match the first 10 results (after every insertion)
DELETE FROM tableName where id NOT IN (SELECT id from tableName ORDER BY id DESC LIMIT 10)
Here is a link to explore more:-
Limit the amount of rows in a room database
Does it work?
This is the correct way to do it:
SELECT expressions
FROM tables
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
LIMIT number_rows [ OFFSET offset_value ];
And here's a real example:
SELECT contact_id, last_name, first_name
FROM contacts
WHERE website = 'TechOnTheNet.com'
ORDER BY contact_id ASC
LIMIT 5;
I got these from https://www.techonthenet.com/sql/select_limit.php.
There could by slight differences in syntax as I do not know which type of SQL you are using.
I have a table which has columns
_id = Primary Key Auto Increment
Title = String
timestamp = long
I have 35 entries in my table. My table can contain only 25 entries at any given time. So that means have of knock off 10 extra entries from my table.
Also 35 entries should be first sorted by timestamp and the last 10 entries should deleted so that i have just 25 recent entries.
Can some please help me with a delete query that first sorts the entries by timestamp and keeps only 25 entries, deleting the rest.
DELETE FROM MYTABLE WHERE _id NOT IN
(SELECT _id from MYTABLE ORDER BY timestamp DESC LIMIT 25)
Keeps the latest 25 entries.
It sounds like you need a FIFO queue in SQL. A table that only stores the most recent 25 (or any other number of) items.
If so, then here is a solution:
http://www.xaprb.com/blog/2007/01/11/how-to-implement-a-queue-in-sql/
Alternative to radashk method:
You can delete one (oldest) record every time you insert a new record, it can be done in DB table trigger on insert:
DELETE FROM MYTABLE WHERE timestamp = MIN(timestamp);
this statement can be wrapped in record count check or something else to make sure that you maintain your minimum record count.
I have a database that stores the rank of an Item.
The rank is an absolute value that will be correct if all the items are taken into account.
If I only need a subset say four of this items it will give me something like:
Rank RowId in the whole Table
---------------
4 114
8 71
70 16
83 7
I now need an int specifying the rank only in the subset where the max rank is the number of items in the subset in my example 1,2,3,4.
Is there a way to achieve this in my sqlite query? I only need one of the ranks in my Activity. I thought of ordering the results of the query by rank and then somehow get the position of item I want to rank at that moment. But how would I achieve this with sqlite?
I tried to create a temporary table and insert the subset into it like this:
CREATE TABLE rank(ID);
INSERT INTO position SELECT ID from items WHERE ITEM_ID = 3 ORDER BY POSITION;
SELECT RowID from rank WHERE ID = 9;
DROP TABLE rank;
This is working in SQLite Manager and will return the correct number. But if I do this in Android in fails saying that there is no table rank while compiling query
07-07 13:35:46.150: ERROR/AndroidRuntime(2047): Caused by: android.database.sqlite.SQLiteException: no such table: rank: , while compiling: SELECT RowID from rank WHERE ID = 9
EDIT: have to agree with #Matt the only way I've been able to do this is to use the temp table approach.
For what it's worth here's what it looks like...
create temp table if not exists <temptable>(Id integer primary key, rank);
insert into temptable(rank) select <column> from <table>;
select * from temptable;
EDIT: Actually that returns the ID associated with the row which isn't sequential so you won't always get 1,2,3,4... I'll have to think of something else. Sorry.
Not sure if I've understood your question. You basically want this?
Id Value
---------------
1 4
2 8
3 70
4 83
So you want to add a pseudo-column as the id no matter what your subset contains?
If that's correct then this should do it...
SELECT RowId, <other columns>.... FROM <table> WHERE <where>
Apologies if I've misunderstood.
You could output your query (ordered by rank) into a temporary table with an auto increment ID.
If you need to read only one row from a subquery you can always execute a limit on it, by providing the offset of how many records to be skipped first, and how much to be returned
so if you want to get 25th row you tell to skip 24, and return 1
select * from (SELECT * FROM table order by rank) limit 24,1