Select only 5 random rows in the last 50 entries - php

I'm just starting with MySQL so I would like to know how can I select only 5 random rows in the last 50 entries of my database? I hope you understand my question.
I'm using PDO and what I have now is this:
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
Then I use a PHP foreach loop...
Thank you

The challenge is determining the last 50 entries. Assuming you have an auto-incremented id, you can do:
SELECT a.*
FROM (SELECT a.*
FROM articulos a
WHERE cat = '$ArtCat'
ORDER BY id DESC
LIMIT 50
) a
ORDER BY RAND()
LIMIT 5;
The key idea is the subquery to get the last 50 entries, and then the final query to get the 5 random rows. The subquery needs to specify how you identify the last 50.

$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() LIMIT 5 ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);

just add limit
i assume in your table you have some date column so we can get last 50
SELECT * FROM (SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY created_tiem desc limit 50 ) t order by RAND() limit 5;

Related

How to get ORDERED and limited rows form MySQL and randomize show order

Is possible, and how to ask MySQL for
SELECT * FROM my_table ORDER by row_id DESC LIMIT 8
get the last 8, newest record from my table, with randomized order for PHP showing method
$results = $mysqli->query($query);
while($row = $results->fetch_assoc()) {
echo $row['my_col_name'];
}
Colud I, and where put the rand() in my SQL query?
Without randomize I get last 8 rows ORDERED 10,9,8,7,6,5,4,3
I want to get in the following order:
9,7,5,4,6,10,3,8;
8,7,3,6,10,9,5,4
...
You can place it inside another select:
SELECT * FROM (SELECT * FROM my_table ORDER by row_id DESC LIMIT 8) t ORDER BY RAND()
Use a subquery:
SELECT t.*
FROM (SELECT t.*
FROM my_table t
ORDER by row_id DESC
LIMIT 8
) t
ORDER BY rand();

How to select the last n row in MySQL?

with this code I select the first 30 row of the table:
SELECT * FROM `table` LIMIT 0 , 30
But how to select the last 30, without changing the order?
It looks like everyone is missing this part:
But how to select the last 30, without changing the order?
First of all, clearly there is no order in the query provided. Assuming the order is ascending on some field this would be the query #DannyFox meant:
SELECT * FROM T
ORDER BY val
LIMIT 0 , 30
Now imagine we have simplified data, such as a, b, c, d, e and that we want only 3 rows instead of 30:
SELECT * FROM T
ORDER BY val
LIMIT 3
If this returns: a, b, c, d, e in each row, then he would expect to get c, d, e in that order. The query everyone is providing:
SELECT * FROM T
ORDER BY val desc
LIMIT 3
Would return e, d, c. The problem with this is that it's actually changing the original order, and the OP say he didn't want to change the order. So technically, the query that would result in c, d, e is:
select * from (
select * from t
order by val desc
limit 3
) s
order by val
Which actually changes the order twice, getting the original order back.
Since you are trying to avoid ordering, then the solution would be to apply it twice.
SELECT *
FROM (
SELECT *
FROM `table_name`
ORDER BY `column_name` DESC -- maybe id?
LIMIT 0, 30
) `table_aliase`
ORDER BY `column_name` ASC
First you need to specify an order:
SELECT * FROM table
ORDER BY some_id ASC -- ascending order
LIMIT 30
If that query returns the first 30 columns, this one will return the last 30:
SELECT * FROM table
ORDER BY some_id DESC -- descending order
LIMIT 30
If you have an auto incremental key/column, say id then here's an example
SELECT * FROM `table` ORDER BY id DESC LIMIT 0 , 30;
Maybe this will work: select * from table WHERE id > ((SELECT MAX(id) from table) - 30);
Nothing is said about an order, so you can not use ORDER BY.
There is a way to get records from a given point, but for that you need to know how many records there are, then use this counted value to provide the limits
SELECT COUNT(*) AS counted FROM table
SELECT * FROM table LIMIT (counted-30),30
Here's my method used with PHP:
$query = "SELECT * FROM table ORDER BY id DESC LIMIT 3";
$res = mysql_query($query);
$results = array();
while($row = mysql_fetch_assoc($res)){
$results = $row[field];
}
// Back to original order based from DB
$results = array_reverse(results);

MYSQL Select from table, get newest/last 10 rows in table

What's the best, and easiest way to do this? My query currently is:
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id
LIMIT 10
This shows the first 10 rows though, not the last 10.
EDIT: I Want the last 10 rows (Which yes, DESC does this) However I want them to be returned in ASCENDING order.
to reverse the order (therefore get last 10 instead of first 10), use DESC instead of ASC
EDIT
Based on your comment:
SELECT * FROM (
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id DESC
LIMIT 10
) AS `table` ORDER by id ASC
If you want the last 10 then just change ASC to DESC
SELECT *
FROM
chat
WHERE
(userID=$session AND toID=$friendID)
OR
(userID=$friendID AND toID=$session)
ORDER BY id
DESC
LIMIT 10
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$limit = 10;
$query = "SELECT * FROM $table";
$resource = mysqli_query($con,$query);
$total_rows = mysqli_num_rows($resource);
$start = $total_rows-$limit;
$query_limit= $query." LIMIT $start,$limit";
First I have set the limit
$limit = 10;
then
$total_rows = mysqli_num_rows($resource);
Here I have taken total number of rows affected.
$start = $total_rows-$limit;
then substracted limit from number of rows to take starting record number
$query_limit= $query." LIMIT $start,$limit";
and then added limit to the query.
For more information about limit see this link
https://www.w3schools.com/php/php_mysql_select_limit.asp
First select the last 10 from the table, then re-order them in ascending order.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 10) sub ORDER BY id ASC

return random 5 records from last 20 records

Simply I have the following
table with records , i want to return randomly 5 records from the last 20 record (order by id desc)
so how we can do it fast
thanks for help.
select * from
(
select * from your_table
order by id desc limit 20
) as lastest_results
order by rand()
limit 5;
Use an inner query to return the last 20, and an outer query to select 5 of them randomly. This may be slow though.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 20) t ORDER BY RAND() LIMIT 5;
This is a slow method but it gets the JOB done:
ORDER BY RAND()
LIMIT 5;
If you are using a large table this can become very slow,
There are multiple alternative that you can read about here
Alternative to order by rand
You need something like
SELECT * FROM table ORDER BY RAND() LIMIT 5;
If you have a time parameter to sort them by for example the last 20 records then use this.
SELECT * FROM table ORDER BY insert_time DESC, RAND() LIMIT 5;
OR
SELECT * FROM table ORDER BY id DESC, RAND() LIMIT 5;

MySql randomize the last 10 rows

I need help on how to randomize the last 10 rows of MySql records.
$mysqld = mysql_query(select * from table where amount > amount2 and code = '$code' order by time DESC limit 1);
From the statement above I need to randomize the last 10 rows ordered by time but limited only 1 to display.
EDIT: In other words, I need to have the table ordered by time and then I need to focus on the last 10 rows. From these last 10 rows, I need to pick one and it must be random, which one I get.
Is this possible?
Thanks
Assuming that time is the time when record was inserted, this will get you the latest 10 rows from the table:
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
Now, you can use the result as a temporary table, sort it randomly (as it's only 10 rows) and return one row:
SELECT * FROM (
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
) AS temptable
ORDER BY RAND()
LIMIT 1
Try....
SELECT * FROM (SELECT * FROM yerTable ORDER BY id DESC LIMIT 10) AS tmp ORDER BY RAND() LIMIT 1
Obviously replace id with any other distinct column if preferred.

Categories