Adam Adam - 2 months ago 8
MySQL Question

MySQL Select Return a certain percentage of a Column (Binary Values)

I have a MySQL User table with a column for Gender - 1 Male & 0 Female. Each Users has a Gender.

I want to be able to return a percentage of male/female when I say return 100 Users. EG: 40 Accounts Male and 60 Accounts Female - 40/60 Split.

How can I do this with MySQL?

select * from users where gender?


thankyou

Answer

If you specifically want to get 40 males and 60 females, then use union all:

(select u.*
 from users u
 where u.gender = 'male'
 limit 40
) union all
(select u.*
 from users u
 where u.gender = 'female'
 limit 40
);