I have 2 tables; 'groups' and 'values'. I am trying to select one random row from the values table, where values.groupid = groups.id
The query I have is:
SELECT *
FROM groups as g
inner JOIN
(
SELECT * FROM values as v WHERE v.groupid = g.id ORDER BY RAND() LIMIT 1
) AS rv ON rv.groupid = g.id
v doesnt seem to have access to g how can i work around this? in regards to the random, I only managed to get the limit 1 solution to work, because there may be deleted value.id's and it is small table (only ever max 200 rows). if I only use join on, and dont have the where clause inside the join, it will select random value from any group, or return nothing. can anyone help me?
edit: this wont work either because I cannot access v from outside this time
SELECT g.*
,(SELECT * FROM v ORDER BY RAND() LIMIT 1)
FROM groups AS g
JOIN
(
SELECT * FROM values
) AS v ON v.groupid = g.id
I see many other question very similar but, none actually try to do a join on the value for each group in effect .. if I am missing one please just mention and I will delete issue, i will continually look at others but in the mean time I am still needing help. thank you