问题
What is the best way to random 1000 row (not duplicate row) from mysql ?
Now i use this
1. get all data(id row) in to array.
2. Random position of array 1000 position.
3. i will get 1000 row (not duplicate row)
But, it's very slow process,
Do you have easy way to get random 1000 row (not duplicate row) ?
回答1:
Well from the comment you are also satisfied with a theoretical answer.
If you have your array with all rows, use array_unique() to get rid of duplicate rows then use shuffle() to mix them up and at the end you can take a slice with array_slice().
EDIT:
You can improve it when you already don't select duplicate rows, then you don't have to use array_unique(). For this use DISTINCT. And if you want to do all in a query you can do something like this:
SELECT DISTINCT column FROM table
ORDER BY RAND()
LIMIT 1000
来源:https://stackoverflow.com/questions/28969663/what-is-the-best-way-to-random-1000-row-not-duplicate-row-from-mysql