954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

display randomly but not repeat

Hello,

i have some problem with this issue. i have to do display results randomly using mysql. but results don't repeat until one cycle completed.

i mean there are 6 results is there....

example first displyed randomly like 2 and 6, next 3 , 4 next 1,5 (one cycle completed)
again displaying randomly like 2 and 4, next 3,5 next 1,6 (cycle completed)

but not repeat result until cycle complete.


Any body help me.

thanks.

muralikalpana
Posting Pro
540 posts since Sep 2009
Reputation Points: 21
Solved Threads: 36
 

You can add an extra integer column to your table. Select two (using LIMIT) random items where you order the query by MIN(extra column) first, and RAND() second. After using the selected two, increment the extra field.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 


sorry i did not get you. if you don't mind please explain. what is the use of extra column.

you mean like this: SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;

muralikalpana
Posting Pro
540 posts since Sep 2009
Reputation Points: 21
Solved Threads: 36
 

Define an extra column (integer).

SELECT * FROM table ORDER BY MIN(extra), RAND() LIMIT 2

Now you get two results. For each of them, increment the extra column by 1. Next time, using the same query, you will get two different results. Suppose you have six items in the table, after the third query, all values in the extra column are set to 1, so all options are equally available again.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 


i understood but i have 1000s of results. then is there any problem will occur on pageloads. i mean slow loading.

muralikalpana
Posting Pro
540 posts since Sep 2009
Reputation Points: 21
Solved Threads: 36
 

I doubt it, but you will notice if it does.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: