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
Related
I am using this query
"select * from SomeTable group by SomeColumn"
It is returns list with accenting order, but i need to same order like in database.
For example the order in database is:
p
a
s
But result is:
a
i
p
Sample
The result need to be like distinct by CityEN but with all columns and order like 1.Paris 2.Amsterdam 3.Istanbul
In Sqlite, each row of a table has a unique rowid, which you can use for sorting.
select * from SomeTable group by SomeColumn order by rowid;
In your statement, add this line to sort the results:
order by min(rowid)
Your query does not enforce any order with ORDER BY clause so no assumption about row order should be made. If you want specific order add i.e. ORDER BY SomeColumn. See docs about all available order options: https://www.sqlite.org/lang_select.html#orderby
By the rules of SQL, you can't count on getting records back in any specific order without specifying an ORDER BY clause in your SQL query.
In practice servers sometimes return values in the order in which they're inserted, in the order of the first index created, or in the order of the primary key--but you can't count on this behavior, and in fact I've seen the behavior change between database maintenance windows or after the database version is upgraded. You definitely wouldn't want to count on a DB engine to give you back records in any particular order if you write a SELECT statement without an ORDER BY clause.
The only real way to get your records back in the order you inserted them is to create a timestamp column and then sort on it during the SELECT. If you don't want to worry about populating that column on INSERT, have that column auto-populate itself with a timestamp (depending on your DB engine).
Please excuse me if its a repeated question. I tried searching here and at google but I couldn't exactly find what I wanted.
I have got two tables A & B.
Table A Fields : id, name, description, rating.
Table B Fields : id, aId (linked to table A), customerId, recommended.
Table A contains my data items for which I'm storing average cumulative ratings provided by users.
Table B stores another attribute for data of Table A. It stores the recommended bit (1 for recommended & 0 for non-recommended).
I want to list all the data from Table A but I want to sort them using recommended bit from Table B. So, if there are 10 records in Table A and 2 records in Table B, while listing all those 10 records, the two from Table B should come first and then the others from Table A. It doesn't matter whether the recommended bit value is a 0 or a 1. While listing the other 8 records from Table A, I want to list the records based on their rating in descending order.
Can someone please guide me in writing this sqlite query for Android app? Thanks in advance!
The left join adds the recommended field to the result set (with a value of NULL if there is no matching B record).
The expression recommended IS NULL or EXISTS(...) returns either 0 or 1:
SELECT DISTINCT A.*
FROM A LEFT JOIN B ON A.id = B.aId
ORDER BY B.recommended IS NULL,
A.rating DESC
Alternatively:
SELECT *
FROM A
ORDER BY NOT EXISTS (SELECT 1
FROM B
WHERE B.aId = A.id),
rating DESC
I have a table named as Attendance, Here is the structure given below.
id fname lname roll_no date time
________________________________________________________
1 Qadir Hussain 08cs18 19/04/2013 8:45am
2 Qadir Hussain 08cs18 19/04/2013 8:50am
_______________________________________________________
i want to insert the record having roll_no = 08cs18 only one time per day. not more than once per day.
I can restrict this via if/else. but is it possible to restrict this via sqlite query?
Edit
Acctually i m making an app of studnets attendance, I m using the QR_code to scan encoded roll_no. once a student scan its card for the first time it should insert the record (i.e id = 1) if user again scan it on the same day it should not insert. means a particular student should have a attendance record only one time per day.
For both databases, make a unique key constraint on roll_no and date.
CREATE UNIQUE INDEX daily_roll_no ON Attendance (roll_no,date)
Then for mysql make your insert with the IGNORE clause. For sqlite, use OR IGNORE.
INSERT IGNORE INTO Attendance ....
INSERT OR IGNORE INTO Attendance ....
mysql:
ignore clause
create index
sqlite:
ignore clause
create index
You can do this in a single SQL:
INSERT OR REPLACE INTO <tablename>
(<identifier>, <time inserted>, column1, column2, ...)
SELECT <identifier>, <time inserted>,
COALESCE(column1,'new value 1'),
COALESCE(column2,'new value 2'), ...
FROM <tablename>
WHERE <identifier> = ...
AND <time inserted> > <now minus one day>
The basic idea is: using INSERT OR REPLACE you can update an existing row or insert a new one depending on one or more conditions. In this case I update the row with the same values, if the row is not older than one day.
If the line is not existing or older than one day, you can assign new values using COALESCE.
I know this is only a kind of outline but may be it helps though .... Cheers!
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
I know all stuff regarding create table and update all, and delete it.
But now i want to delete some tables whos created date is less then 2 weeks.
So can anybudy tell me how to fetch such type of tables and from there i want to delete the table whoes date is less then 2 weeks.
Please help me regarding it. . .
Thanks.
For this you have to maintain one new table (creation_date) with all tables name and their creation dates
Whenever you want to delete table whose date is less then 2 weeks at that time first fetch the system's date & time. After that count the date less then 2 weeks from current date;
Using the 2 weeks later date query in the table whose contain all table name as below
Select table_name From creation_date Where crnt_date < date
Using this you can get all the table names whose created before 2 weeks
After this you can fire query for each table for delete that table like below
Drop Table table_name
AFAIK sqlite (I guess you're talking about it) does not keep table creation date as part of the database metadata accessible trough the sqlite_master table. But it would be trivial to create an extra table to keep track of creation dates if this is what you need.