Code:
data in myTable7

(id) sex name
(1)  0   Mary 
(2)  1   Tom 
(3)  1   John 
(4)  0   Judy 
(5)  0   Jane 
(6)  1   Jack 
(7)  1   Andy 
(8)  1   Tim 
(9)  0   Liz 
(10) 0   Andrea 
(11) 1   Mikey 
(12) 0   Judy 
(13) 0   Anna

data in myTable8 

myGroup id
(1)     8
(1)     5
(2)     1
(2)     7
(2)     11
(3)     9
(3)     13
(4)     3
(4)     2
(4)     10
(4)     12
I have data in myTables like the above.

The code below produces the result below.
Code:
code

select name, myGroup
from myTable8
left join myTable7 on myTable7.id=myTable8.id
order by myGroup

result

(1) Tim
(1) Jane
(2) Mikey
(2) Mary
(2) Andy
(3) Anna
(3) Liz
(4) Judy
(4) Tom
(4) John
(4) Andrea
I like to produce only one randomly from each myGroup.
The following code is for it and the following result is one of the results.
Code:
code

(select name, myGroup
from myTable8
left join myTable7 on myTable7.id=myTable8.id
where myGroup=1
order by rand() limit 1)
union
(select name, myGroup
from myTable8
left join myTable7 on myTable7.id=myTable8.id
where myGroup=2
order by rand() limit 1)
union
(select name, myGroup
from myTable8
left join myTable7 on myTable7.id=myTable8.id
where myGroup=3
order by rand() limit 1)
union
(select name, myGroup
from myTable8
left join myTable7 on myTable7.id=myTable8.id
where myGroup=4
order by rand() limit 1)
union

order by myGroup

one of the results

(1) Tim
(2) Mary
(3) Anna
(4) John
The one of the results has a problem, i.e it doesn't balance the male and female.

It often produces 3 females and one male like the below.

(1) Jane
(2) Mary
(3) Anna

(4) Tom

It often produces 3 males and one female like the below.

(1) Tim
(2) Andy

(3) Liz
(4) Tom

Futhermore, it sometimes produces all females like the below.

(1) Jane
(2) Mary
(3) Anna
(4) Andrea



I like to make it that the result always has balancing the number of male and the number of female, i.e. two females and one males randomly.