To write an SQL statement, I need to query the latest 30 messages in the message table, and then display these 30 messages at random.
Reply to discussion (solution)
What is random display?
What is random display?
First, I want to display five pieces of information at the front-end, but these five pieces of information, you must randomly retrieve 5 of the latest 30 entries in all information in the message table.
Such SQL statements cannot be implemented.
SQL statements can also meet the requirements. if the table is large, it may be a little slow.
SQL statements can also meet the requirements. if the table is large, it may be a little slow.
Can you give an example.
Read 30 data records first. use php to display them randomly.
Create a temporary table, store the latest 30, and then use select * form a order by rand () limit 5
Create a temporary table, store the latest 30, and then use select * form a order by rand () limit 5
Isn't it slow to execute this once every refresh?
Therefore, write a program in php to randomly extract 5 entries, or write a specific table to put the latest 30 entries.
Order by rand () is not recommended
Yes
Select * from xxxx order by timefield desc limit 30
Put the result into the array and shuffle it.
Therefore, write a program in php to randomly extract 5 entries, or write a specific table to put the latest 30 entries.
I was wondering if I could use SQL? Because the record set I read is a class.
SQL can be achieved, but it is really not efficient
select * from (SELECT * FROM xxx order by xxx desc limit 30) t order by rand()
Not required? :
Select <你的字段> From (select <你的字段> From <你的表> Order by uid limit 30) as t order by rand () limit 5
According to select * from (SELECT * FROM xxx order by xxx desc limit 30) t order by rand (), the data originally obtained is a set of classes, so it is converted into SQL and then spliced, and then executed to return an array.
I don't think the problem is big. it's not the full table order by rand.
SQL can be achieved, but it is really not efficient
SQL code? 1 select * from (SELECT * FROM xxx order by xxx desc limit 30) t order by rand ()
I don't think the problem is big. it's not the full table order by rand.
Reference the reply from ustb on the 13th floor: SQL can be done, but it is really inefficient.
SQL code? 1 select * from (SELECT * FROM xxx order by xxx desc limit 30) t order by rand ()
Use less nested queries ?...
I don't think the problem is big. it's not the full table order by rand.
Reference the reply from ustb on the 13th floor: SQL can be done, but it is really inefficient.
SQL code? 1 select * from (SELECT * FROM xxx order by xxx desc limit 30) t order by rand ()
Agree!
If you randomly display 30 records, there will be no special effect. The text will be the same in the end.
If you only display 10 entries, then 30 entries are randomly displayed. The random effect is changed.
Just try again.
select * from (SELECT * FROM xxx order by xxx desc limit 30) t order by rand() limit 5
SQL can be achieved, but it is really not efficient
SQL code? 1 select * from (SELECT * FROM xxx order by xxx desc limit 30) t order by rand ()