Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mysql - how to select random unique records on each execution of the SQL Query

I have a table "masterurls" it has morethan 1 million records. I want to fetch random records each time the query executed. It should not have any of the records that were fetched in previous executions. I'm already have this query:

SELECT m.url FROM masterurls ORDER BY RAND() LIMIT 200

The problem is the above query returns only first 200 hundred records and randomizes it each time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How are you going to know if the url is already accessed before. My best suggestion would be setting a flag to know this in the table. Add a field like view in the table which will accept two values 1 or 0, 1 for already accessed and 0 for not accessed. Then you could use

SELECT m.url FROM masterurls m WHERE view='1' ORDER BY RAND() LIMIT 200;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...