如何获得女性总数,男性总数以及未订购产品的男性和女性用户的总和。结果应如下所示:
total male | total female | total
---------------------------------------
31 | 24 | 55
---------------------------------------
小提示帮助http://sqlfiddle.com/#!9/3a0e30/36
上一个代码:
select p.*,
(select count(*)
from users u
where u.uid not in (select o.uid from orders o where o.productid = p.productid) and
u.gender = 'Male'
) as NumMales,
(select count(*)
from users u
where u.uid not in (select o.uid from orders o where o.productid = p.productid) and
u.gender = 'Female'
) as NumFemales
来自产品p;