What is the best way to random 1000 row (not duplicate row) from mysql?

僤鯓⒐⒋嵵緔 提交于 2020-01-17 01:38:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!