My table has around 10k rows. How can I query select 1 random row of the last 10 added rows? I have a date column that is datetime of when the row was added.
This is one I use to get the last 10 rows:
SELECT id FROM mytable order by date desc LIMIT 10
I know I can do SELECT id FROM mytable ORDER BY RAND() LIMIT 1 but that just choose any random row, not from the last 10 rows.
One method uses a subquery:
SELECT t.*
FROM (SELECT id
FROM mytable
ORDER BY date DESC
LIMIT 10
) t
ORDER BY rand()
LIMIT 1;
This version uses the syntax conventions for MySQL.
You can use select * in the subquery to fetch the whole row.
Related
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
I have posts which has fields (ID,title,date) What I'm looking for it to select rows order by date desc just limit for 25 records
lets say if we run that one it will show the last row with ID (600) like this:
(600,601,602,.....,625)
so I want after that to select another 25 records but min(id) before the last one (600) so it will be like this
(575,576,577,.....,599)
For example consider that the first result gives the descending last five IDs.
SELECT idprocess FROM process ORDER BY idprocess DESC LIMIT 5
248034
248033
248032
248031
248030
You could use a SELECT into a temporary table to mess with your result set's ORDER BY and LIMIT. The sub-query LIMIT should be the sum of the records you want back and how many you want skipped off the end.
SELECT t1.idprocess FROM
(SELECT idprocess FROM process ORDER BY idprocess DESC LIMIT 10) AS t1
ORDER BY idprocess ASC LIMIT 5
248025
248026
248027
248028
248029
I have been trying to figure out/ searching about how to do this but failed.
I have table with id as primary key and auto incremental and and a column clickers which counts member clicks. What I'm trying to do is average the value of "clickers" of the last 30 records except last very last 1
SELECT AVG(clickers) AS avgClickers FROM sitelogs_daily_stats ORDER BY id DESC LIMIT 30
But this code includes the last records. I want to skip that one.
Any help would be very helpful.
Wrap it in derived query:
SELECT
AVG(clickers) as avgClickers
FROM
(
SELECT clickers
FROM sitelogs_daily_stats
ORDER BY id DESC
LIMIT 1,29
) AS t1
Notice in the derived query, we are selecting the records of interest, and then averaging them in the outer query.
SELECT AVG(clickers) AS avgClickers
FROM sitelogs_daily_stats
WHERE id BETWEEN
(SELECT MAX(id)
FROM sitelogs_daily_stats) - 29 AND
(SELECT MAX(id)
FROM sitelogs_daily_stats) - 1
I am using this query:
SELECT * from likes GROUP BY url ORDER BY count(*) DESC LIMIT 6
to fetch most liked record from my table 'likes'. It is working perfect for fetching most liked content of all time.
But now to I want to select the 6 most liked record from the last 100 records.
What will be the query for it ?
SELECT * FROM (select * from likes order by date desc limit 100) xx
Group by URL order by count(*) limit 6
Obtain the primary keys of the last 100 entries and narrow your query to it. Probably extremely easy if you have auto-increment keys.
SELECT * from likes
GROUP BY url
ORDER BY count(*) DESC
WHERE ID > MAX(ID)-100
LIMIT 6
How can I select a single random entry from a MySQL database using PHP?
I want to select the Author, AuthorText, and Date?
SELECT Author, AuthorText, Date FROM table ORDER BY RAND() LIMIT 1
Take a look to this interesting article:
“Do not use ORDER BY RAND()” or “How to get random rows from table?”
ORDER BY rand() LIMIT 1
will sort all the rows in the table, which can be extremely slow.
Better solution : say your table has the usual primary key auto-increment field, generate a rendom number between min(id) and max(id) and select the closest id.
It will not be as random as a "true" random selection, because a id after a large hole of deleted ids will have a higher probability of being chosen. But it will take 50 µs instead of 2 seconds if your table is large...
SET #t = (SELECT FLOOR(a + (b-a)*rand()) FROM (SELECT min(id) as a, max(id) as b FROM table)
SELECT * FROM table WHERE id>#t ORDER BY id LIMIT 1;
You can order by a random & restrict to 1 row as follows:
select
author, authortext, date
from bookstable
order by rand()
limit 1