简体   繁体   中英

PHP/Mysql Order by and Rand()

How to write a mysql query with order by points Desc and rand().

Query

SELECT * FROM users ORDER BY points DESC

I want to sort data based on highest points and random too. I am making a leaderboard. If 2 persons have the same point just for sake of difference I want to shuffle the order. Whats the another solution.

You could give each result a random number and order the result first by points DESC then by the random value:

SELECT *, RAND() random FROM users ORDER BY points DESC, random ASC

Probably there's also the solution with:

SELECT * FROM users ORDER BY points DESC, RAND()

But I'm not sure about that one.

dear SELECT * FROM users ORDER BY points DESC, RAND() this will not work. because only we can sort the data on one order. you can only use RAND() or based on desc not both

Query provided by @timeSpinter is right, you can use one criteria to ordered data.

like

SELECT * FROM users ORDER BY RAND()

or

SELECT * FROM users ORDER BY DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM