MySQL Random Select Query with limit returning different number of results (undesired)

SnareChops

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 ?
Akhil
select name from (SELECT Rand() temp, Name 
FROM RandomNames) a order by  temp  
LIMIT ?

How about this?

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

MySQL Error: The used SELECT statements have a different number of columns

분류에서Dev

Generating random number with different digits

분류에서Dev

How to limit number of filtered results in ActiveAdmin

분류에서Dev

How can I limit the results in a PagingAndSortingRepository @Query?

분류에서Dev

Limit query results when counting entities

분류에서Dev

How to limit the results of a SQL query by group

분류에서Dev

MySQL Full Text Search Returning 0 Results

분류에서Dev

mysql select recursive query

분류에서Dev

MySQL Case select query

분류에서Dev

How to select different users filtering of certain results?

분류에서Dev

PHP Output Multiple SQL Query Number of Results

분류에서Dev

mean of non zero elements - why are these two attempt returning different results?

분류에서Dev

Php MySQL changing limit by select box selection

분류에서Dev

LEFT JOIN LIMIT로 MYSQL SELECT 1

분류에서Dev

MySQL MATCH AGAINST query not returning certain requests

분류에서Dev

Select results from one query to another

분류에서Dev

NEST elasticsearch.NET search query not returning results (part 2)

분류에서Dev

MySQL get results for different timespans and group

분류에서Dev

Concatenating and grouping results from a mysql query

분류에서Dev

Different random number output at the same time (Unity C#)

분류에서Dev

How to select a specific number of random elements from a set?

분류에서Dev

MySQL Select only returning one row from eclipse

분류에서Dev

MySQL Query returning all dates instead of specified year and month clauses

분류에서Dev

MySQL query is slow only when using ORDER BY field DESC and LIMIT

분류에서Dev

mysql query, limit 및 order by, 잘못된 답변

분류에서Dev

Mysql Insert query with values and select as subquery

분류에서Dev

MySQL Best select query from two table

분류에서Dev

Mysql: First Select then Update in One Query

분류에서Dev

Echo Result of MySQL SELECT Query in PHP

Related 관련 기사

  1. 1

    MySQL Error: The used SELECT statements have a different number of columns

  2. 2

    Generating random number with different digits

  3. 3

    How to limit number of filtered results in ActiveAdmin

  4. 4

    How can I limit the results in a PagingAndSortingRepository @Query?

  5. 5

    Limit query results when counting entities

  6. 6

    How to limit the results of a SQL query by group

  7. 7

    MySQL Full Text Search Returning 0 Results

  8. 8

    mysql select recursive query

  9. 9

    MySQL Case select query

  10. 10

    How to select different users filtering of certain results?

  11. 11

    PHP Output Multiple SQL Query Number of Results

  12. 12

    mean of non zero elements - why are these two attempt returning different results?

  13. 13

    Php MySQL changing limit by select box selection

  14. 14

    LEFT JOIN LIMIT로 MYSQL SELECT 1

  15. 15

    MySQL MATCH AGAINST query not returning certain requests

  16. 16

    Select results from one query to another

  17. 17

    NEST elasticsearch.NET search query not returning results (part 2)

  18. 18

    MySQL get results for different timespans and group

  19. 19

    Concatenating and grouping results from a mysql query

  20. 20

    Different random number output at the same time (Unity C#)

  21. 21

    How to select a specific number of random elements from a set?

  22. 22

    MySQL Select only returning one row from eclipse

  23. 23

    MySQL Query returning all dates instead of specified year and month clauses

  24. 24

    MySQL query is slow only when using ORDER BY field DESC and LIMIT

  25. 25

    mysql query, limit 및 order by, 잘못된 답변

  26. 26

    Mysql Insert query with values and select as subquery

  27. 27

    MySQL Best select query from two table

  28. 28

    Mysql: First Select then Update in One Query

  29. 29

    Echo Result of MySQL SELECT Query in PHP

뜨겁다태그

보관