0

I have Table A with 15,000 rows each having a unique ID and Table B with 10,000,000 rows with multiple rows for each unique ID in Table A. I need to check Table B for each ID from Table A and select 1 random row from each of all matching IDs. So once the query runs I will end up with 15,000 random rows from Table B (each with a unique Table A ID).

I'd assume this is too intense for RAND() alone or a loop so it's got to be an involved select.

Looked to this but it only involves one table http://explainextended.com/2009/03/01/selecting-random-rows/

Not able to adjust it to do what I need.

hellodolly
  • 149
  • 1
  • 3
  • 11
  • See also http://stackoverflow.com/questions/1244555/how-can-i-optimize-mysqls-order-by-rand-function – derobert Oct 05 '11 at 21:27

1 Answers1

0

what about something like this?

select distinct b.a_id from b inner join a on a.id = b.id order by rand(10000000)
dstarh
  • 4,976
  • 5
  • 36
  • 68