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.
Related
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;
I am working on user activity so in that case i need to check last activity of a user in last 30 rows.
I tried using this but this will look in all table row but as i need to check only last 30 i need help in that thanks.
$CheckQuery = mysql_query("SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT 0 , 1");
while($row = mysql_fetch_assoc($CheckQuery))
{
// some function
}
This is what i wanted to know :)
select *
from (select *
from activities
order by `id` desc
limit 30) t
where got_id = '$user->id'
limit 1
AS i said : i need to check last activity ( not activites ) of a user in last 30 rows.
This is the correct approach, you must only use limit 30 instead
SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT 30
Update:
If you want the last activity of a user in the last 30 activities overall instead, you must first subselect the last 30 activities and pick the user's last activity from that
select *
from (select *
from activities
order by `id` desc
limit 30) t
where got_id = '$user->id'
limit 1
but this might give an empty result set, if the user wasn't active recently.
Just select the last 30 rows by counting in a descending order..
Check out this query:-
"Select * from activities WHERE got_id='$user->id' ORDER BY `id` DESC limit 30"
SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT 30"
See here for limiting your query: http://dev.mysql.com/doc/refman/5.6/en/limit-optimization.html
Try this,
$CheckQuery = mysql_query("SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT 30");
My code get the last 10 values from a table. This table has te structure id,text and by the moment it has 20 rows. I use this piece of code
<?php
$query = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT 10");
// Fetching data stuff here
?>
and it returns the data for IDs 11-20. (The last 10 of the 20)
I want to get the previous 10 values from this 1-10 via AJAX. I thought maybe this will work
$previous_id= $_GET["last"]; // This time it will be 11
mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $previous_id, -10");
But I'm doesn't, any suggestion?
Thanks
PS: This is not a gimme teh codez question, I just want to know how to make that query work.
If I understand you correctly you want to get the 10 rows before the row with id = $previous_id, ordered by descending ID.
If that's the case your best bet would be to use a WHERE condition. As you are ordering by id DESC you want the first 10 rows with id > $previous_id:
SELECT * FROM `table` WHERE `id` > $previous_id ORDER BY `id` DESC LIMIT 10
These will be the 10 rows before $previous_id in your original query.
Simply use positive number instead of -10:
"SELECT * FROM `table` ORDER BY `id` DESC LIMIT $previous_id, 10"
Let's suppose $previous_id is 5, the query would be:
"SELECT * FROM `table` ORDER BY `id` DESC LIMIT 5, 10"
The returned rows will be starting from 5 and 10 records.
I want to return only the last 15 rows in my table, then the 15 before that.
Unfortunately while($rows = mysql_fetch_assoc($result)) where the query is SELECT * FROM table returns the data in all rows.
I thought about doing something like:
In my insert script
SELECT * FROM table then $selection_id = mysql_num_rows($result)-14 before inserting any data, then adding column named selection_id which would contain $selection_id, thus each set of 15 rows would have the same selection_id.
In my select script
SELECT * FROM table then $num_rows = mysql_num_rows($result)/15 then SELECT * FROM table WHERE selection_id='$num_rows' and SELECT * FROM table WHERE selection_id='$num_rows-1'.
I could then perform while(..) on both results as usual.
However, I'm not sure this is the most efficient way (chances are it's not), so if not, I'd really appreciate some suggestions to cut down the amount of code I'll have to use :)!!
Use a LIMIT clause in your query, order by your auto-incrementing primary key in descending order. E.g.
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 0,15
...will get the last 15 rows, and:
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 15,15
...will get the 15 rows before that.
Selecting the last 15 rows:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 0,15
Selecting the 15 rows before the previous ones:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 15,15
And you can continue in a while cycle.
You need to check out mysql LIMIT. To get the last 15, you'd need to know the number of total rows.
$offset=$rowcount-15;
$sql="SELECT * FROM mytable LIMIT $offset,15";
This is just for example, you'd want to make sure there are at least 15 rows, I'm not sure how mysql would deal with a negative offset. I'll let you figure out how to count the rows.
Edit:
Oh, haha, you could also just sort it descending, that will save you having to query twice.
SELECT * FROM mytable ORDER BY id DESC LIMIT 15;
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;