Select records older than latest 100 records from database - sql

My issue here is that i want to populate all the records except latest 100 records in the database. What could be the best approach to accomplish this?

Try using limit cause
SELECT *
FROM Table
order by id desc
limit 101 , totalrecords
Here id is auto increment field of your table

How about something like
SELECT t.*
FROM Table t LEFT JOIN
(
SELECT ID
FROM Table
ORDER BY ID DESC
LIMIT 100
) top100 ON t.ID = top100.ID
WHERE top100.ID IS NULL
Where ID would be the column to identify the order (latest) and Table from where you wish to select

SELECT *
FROM Table
WHERE ID < (SELECT ID
FROM Table
ORDER BY ID DESC
LIMIT 99, 1)

Related

Select last 2 record against record ID in SQL

I want to know if there is a query for selecting last two inserted records against record ID.
For example we can select only 1 top record by using this query:
select max(colName) from tableName
But what query could be for this:
select "Last two records of" colName where id = 1
so if we have 100 records in the table and we have 10 records against id number 1, then we should get the last two inserted records against the id number 1.
Please help me if anybody understood my question.
Note: id is not unique key OR primary key in the table from where I want to get the record.
Script should be something like below-
SELECT TOP 2 *
FROM tableName
WHERE id = 1
ORDER BY colName DESC
what about
select colName from tablename where id in (x,x-1)
Assuming you are using auto increment for the primary key
if you have the column, created_at you can do something like
select * from table where id = 1 order by created_at desc limit 2
that should give you the most recent inserted elements. Are you using postgres, mysql, oracle?
select top (2) * from <myTable> where id = 1 order by id DESC
This will return last 2 inseted rows where Id = 1

How Can i conditions of the query as a column in the data

i want get a SQL like the pic~
now i want get the result by Top 3 projectId (4,3,2) total 8 result.
so how can i mody my sql?
( the SQL in real project is so complex~,about 10 my DBTable and 10 SAP table~ i concerned about efficiency so i don't want use temporary table)
can u help me~?
if you are using SQL Server, use TOP
SELECT *
FROM table1
WHERE ProjectID IN
(
SELECT DISTINCT TOP 3 ProjectID
FROM table1
ORDER BY ProjectID DESC
)
ORDER BY ProjectID Desc
SQLFiddle Demo
If using MySQL then:
SELECT * FROM TABLE1
ORDER BY PROJECTAID ASC
LIMIT 10;

Row with the highest ID

You have three fields ID, Date and Total. Your table contains multiple rows for the same day which is valid data however for reporting purpose you need to show only one row per day. The row with the highest ID per day should be returned the rest should be hidden from users (not returned).
To better picture the question below is sample data and sample output:
ID, Date, Total
1, 2011-12-22, 50
2, 2011-12-22, 150
The correct result is:
2, 2012-12-22, 150
The correct output is single row for 2011-12-22 date and this row was chosen because it has the highest ID (2>1)
Assuming that you have a database that supports window functions, and that the date column is indeed just date (and not datetime), then something like:
SELECT
* --TODO - Pick columns
FROM
(
SELECT ID,[Date],Total,ROW_NUMBER() OVER (PARTITION BY [Date] ORDER BY ID desc) rn
FROM [Table]
) t
WHERE
rn = 1
Should produce one row per day - and the selected row for any given day is that with the highest ID value.
SELECT *
FROM table
WHERE ID IN ( SELECT MAX(ID)
FROM table
GROUP BY Date )
This will work.
SELECT *
FROM tableName a
INNER JOIN
(
SELECT `DATE`, MAX(ID) maxID
FROM tableName
GROUP BY `DATE`
) b ON a.id = b.MaxID AND
a.`date` = b.`date`
SQLFiddle Demo
Probably
SELECT * FROM your_table ORDER BY ID DESC LIMIT 1
Select MAX(ID),Data,Total from foo
for MySQL
Another simple way is
SELECT TOP 1 * FROM YourTable ORDER BY ID DESC
And, I think this is the most simple way!
SELECT * FROM TABLE_SUM S WHERE S.ID =
(
SELECT MAX(ID) FROM TABLE_SUM
WHERE CDATE = GG.CDATE
GROUP BY CDATE
)

how do i select the last 10 records added?

i am running mysql and i would like to display the last 10 records that were added. what is the select statement for this?
If you have an auto-incrementing ID, you can do this:
SELECT * FROM my_table ORDER BY id DESC LIMIT 10;
If you don't, you'll need some criteria you can order by. An insertion date or something. The LIMIT 10 clause is what you're looking for here.
If you have an autoincrementing ID you can use this:
SELECT *
FROM yourtable
ORDER BY id DESC
LIMIT 10
If you have a column added_datetime that is set to the time of the insert then you can use that instead:
SELECT *
FROM yourtable
ORDER BY added_datetime DESC
LIMIT 10

How to select a random record from a MySQL database?

I am using the following query to select 1 random record -
SELECT name FROM table WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM table ) ORDER BY id LIMIT 1
but it gives me the same set of records every time I call it. How do I get better random record?
Try this:
SELECT * FROM tableName ORDER BY RAND() LIMIT 1