I have a MySQL table with names in it. I am trying to get a random list of names in random order from the table, without a big hit to performance. I found a query that does this and have implemented it, but with one flaw... Say I have 10 names in the database and I request 2 random names using the query. I usually get 2 back, but sometimes I only get one. This of course gets worse if I ask for say 9 names back. I'll get 5, then the next time 8, then 9, then 2. I believe that this has something to do with the script selecting a random id and then going up from there, the issue is that I want to have exactly the number of names back that I request each and every time (Unless the database doesn't contain enough names, in which case it returns all of the names, but in a random order.)
Here is the query I am using:
SELECT Name
FROM RandomNames AS r1
JOIN (
SELECT (Rand() * (SELECT MAX(ID) FROM RandomNames)) AS ID
) AS r2
WHERE r1.ID >= r2.ID
ORDER BY r1.ID AS C
LIMIT ?
select name from (SELECT Rand() temp, Name
FROM RandomNames) a order by temp
LIMIT ?
How about this?
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다