I have a table A with a foreign key to table B and I need to select 10 random fields from A such that fields in consecutive pairs have different b_id, i.e. a valid result would be:
(x1, x2) (x3, x4) ... (xn, xn+1) where xi are the selected fields and if we name fi the foreign key of xi to B, then fi should different of fk+1. I have tried a really awful query which I don't like at all and I believe slow.
This is my current query:
select a1.b_id, a1.x, a2.b_id, a2.x
from A a1, A a2
where a1.b_id <> a2.b_id
order by rand()
limit 5