Haza Haza - 1 year ago 62
MySQL Question

How to get random rows that are diffrent?

This is my mysql table
table name is kl_schedule

id time show
___ ____ ________
1 2:00 dogs and cats
2 3:00 dogs and cats
3 4:00 dogs and cats
4 5:00 dogs and cats

I want to be able to pull the first two rows from the table. For example,
I have two divs. For div one, I want to have two random rows, and for div two I want to have two different random rows. How would I do that?

I took a look at this question but it did not work for me because I need it to get both pairs of random rows that are not the same. That is why that code did not work, it would also be fine if it shows the first 2 then the last 2 - as long as it shows two that are different.

Answer Source

Hi I think you want two random rows

SELECT id,`time`,`show` FROM kl_schedule order by rand() LIMIT 2
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download