Getting the last three records from SQLite database - android

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

Related

SQL Query, How to find the first and last value in the given table need by sequential order with conditions

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>

How to store only 10 last records to room?

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.

Query to pull every 10 data from the table in SQLLite

I am trying to pull data starting from today, to last 10 transactions i.e. I am getting the first 10 transactions and then I want to apply scroll up / down to move to previous 10 / next 10 transactions.
SELECT * FROM table_transaction LIMIT 10
I tried the above query this gives a LIMIT of 10 starting from the top (Today's to last 10 transaction details)
How do I implement the above logic?
Can somebody help me fix this?
if you want get 10 starting from the top
SELECT * FROM table_transaction LIMIT 0,10
and if you want next 10 item:
SELECT * FROM table_transaction LIMIT 1,10
You can use the ROWID column of the sqlite tables. This column is automatically managed by the sqlite.
Sqlite Documantation
Use OrderBy Clause for geting last n number of transaction -
SELECT * FROM table_transaction LIMIT 10 ORDER BY COLUMN_NAME DESC
above query returns last 10 inserted transaction.
For Scrolling you have to create logic create an integer variable say 'last_order_id' and assign it to max order id.
In your query pass last order id and every time you execute query update 'last_order_id'.
So your updated query will be -
SELECT * FROM table_transaction LIMIT 10 ORDER BY COLUMN_NAME DESC INNER JOIN table_transaction.order_id<last_order_id;
hope it will help you.

android get last 5 records

I want to get the latest 5 records in my table, so far i tried this but, it did not work out very well. So, what is the cleanest and efficient way to get last 5 records in the table ?
"select * from (select * from People order by Date DESC limit 5) order by Date ASC;"
Your query works just fine.
To make it efficient, ensure that there is an index on the Date column; then SQLite will just read the last five entries from the index and the table and does not need to scan the entire table.
If this table has an autoincrementing ID column, and if "latest" means the insertion order, then you can use that ID for sorting; this will be as efficient as your original query with an index on Date:
SELECT * FROM (SELECT * FROM People
ORDER BY _id DESC
LIMIT 5)
ORDER BY Date ASC

How to display last three rows of a database table in android?

I am developing an android application in which i have a database table and i want to display only last three rows from the database how can i achieve it ?
As a SQL table has no inherent order, you have to specify something you want to order by, like a date or id and limit the result to 3.
SELECT * FROM yourTable ORDER BY date DESC LIMIT 3

Categories

Resources