Selecting a random result that changes each time the page refreshes |
[eluser]mikegeorgeff[/eluser]
I've been trying to figure this one out for a while now. I have a table filled with client testimonials, and I want to place one random result into the footer that will change each time the page is refreshed.
[eluser]CroNiX[/eluser]
There is no efficient way to do this in mysql AFAIK. From what I've read, the best way is to select MAX(id) in one query, then use php to get a random number between 1 and that number and then do a 2nd query to get the record. However, that assumes no rows have been deleted.
[eluser]maldoinc[/eluser]
You should use the following query to get the desired results Code: select * from testimonials order by rand() limit 1
[eluser]CroNiX[/eluser]
Try doing that on a large table. It can take forever to get a result. http://www.webtrenches.com/post.cfm/avoid-rand-in-mysql
[eluser]maldoinc[/eluser]
Considering the table is for testimonials it should be fine, unless his testimonials table has 1k+ records.
[eluser]InsiteFX[/eluser]
Every time the page is refreshed it will access the database now think what will happen if you have a 1000+ users online! |
Welcome Guest, Not a member yet? Register Sign In |