I'm having the following table: Table paragraph ID | text | number _________________ 1|text1|1 2|text2|1 3|text3|1 4|text4|2 5|text5|2 6|text6|3 7|text7|3 8|text8|3 9|text9|3 I need to select 3 random rows that have DISTINCT number. I have tried something like SELECT id,text,number FROM paragraph WHERE number = DISTINCT ORDER BY rand() LIMIT 3 PHP: , but it gives errors. Anyone willing to help? Thanks
SELECT DISTINCT number, id, text, number FROM paragraph ORDER BY rand() LIMIT 3 Code (sql): Try this.
Thanks, it seems to work, but i think there is a problem with the rand() , as it returns always the same 3 numbers. It returns different rows but from the same numbers.
in the long run it's faster to just select the distinct id's and then get the random generated ones via PHP then re-query the database to get the ones that were selected. less overhead that way looking from a performance standpoint. of course with only a result set of 3 rows it really wouldn't matter but just for future reference.