I do know how to access last N records from the table i.e,
SELECT * FROM table_name ORDER BY auto_incremented_id DESC LIMIT N;
There are N records means and table records keep incrementing every day, each record having unique serial ID from 1 to N.
now i want to retrieve 10 records of last N-10 rows.
please consider the example
Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record N-M
Record N-2
Record N-1
Record N
How do i can retrieve N-2 to N-M rows
The solution required for an website - 1st page displays last 10 records, 2nd Page displays last next 10 records from the reverse except rows are displayed in first page and it goes on up to 1st record of the table.
you can play with ordering and your limit conditions
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT N) as temp ORDER BY auto_incremented_id ASC LIMIT M
assuming as you say a paging size of 10
first page:
SELECT * from as temp ORDER BY auto_incremented_id DESC LIMIT 10;
second page:
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT 20) as temp ORDER BY auto_incremented_id ASC LIMIT 10;
third page:
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT 30) as temp ORDER BY auto_incremented_id ASC LIMIT 10;
For dynamically limiting the output rows you can use inline views (as MySQL doesn't support common table expressions using WITH clause). An example code is as follows:
SELECT *
FROM (SELECT id, name
, #i := #i + 1 as result
FROM table_name
, (select #i := 0) temp
ORDER BY id) v
CROSS JOIN (SELECT COUNT(id) AS M FROM table_name) w
WHERE result BETWEEN w.m-2 AND w.m;
Fiddle.
Related
function rand(); work slow when have millions rows,
I have table 'banners'
id image type
1 name 1
2 name 4
3 name 76
19999999 name 3
need to select
select * from banners where type = 1 order by rand() limit 1
if no result then
select * from banners where type = 3 order by rand() limit 1
if no result then
select * from banners order by rand() limit 1
I try
select * from (
(select * from banners where type = 1 order by rand() limit 1) union
(select * from banners where type = 3 order by rand() limit 1) union
(select * from banners order by rand() limit 1)
) as r limit 1
but is very slow!
You can combine your queries:
select *
from banners
order by type=1 desc, type=3 desc, rand()
limit 1;
but that's still likely to have to read the entire table.
Do you have INDEX(type)? If so, that might help your first 2 SELECTs.
Provide SHOW INDEXES. It may indicate that the "cardinality" of type is such that it will 'always' or 'never' do a table scan. Also, provide EXPLAIN SELECT ... for each case.
If the index is useful and there are not many rows with type=1, that query will be relatively fast. Etc.
This discusses several ways to efficiently pick a random row. See which one applies for each of your 3 cases. Then write a Stored Routine that works something like
BEGIN
... type=1
IF a row found
RETURN...
... type=1
IF a row found
RETURN...
Return a random row from the whole table
END
select * from banners
order by Case
when Type=1 then 1
when Type=3 then 2
else 3
end asc, rand()
limit 1;
I wish to get min(somecolumn) from table from first n rows in MySQL. What is the best query to get the result?
So far I found
select min(a.column) from (select column from table limit 2000) a
select min(a.column) from table a INNER JOIN (select column from table b limit 2000) on a.pricolumn = b.pricolumn.
select min(t.columnName) from tableName as t limit 10 (Here n limit is for first 10 rows as example)
OR
select outerTable.columnName from (select distinct t.columnName from tableName as t order by t.columnName asc limit 10) as outerTable limit 1,1
Try the below syntax:
SELECT min(columname) FROM tablename limit n
I have a table that has transactions with a datetime column. I'm trying to select the last 'n' records (i.e. 20 rows) but have it sorted oldest to newest.
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20;
Gives me the 20 most recent, but in the opposite order.
Is this possible in one query, or will I have to do a query to get total rows and then adjust the limit based on that so I can do the table.datefiled ASC and then limit (total rows - n), n
Building a SELECT around your original SELECT and convert this to a derived table should do it
SELECT t.*
FROM (
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20
) t
ORDER BY t.datefield
I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query
Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1