国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

mysql - [SQL]如何統(tǒng)計(jì)每班男女生人數(shù)?
大家講道理
大家講道理 2017-04-17 12:04:51
0
3
6997

表User有字段:name,sex,classId,比如有如下記錄:
a, 男, 1
b, 男, 1
c, 女, 1
d, 女, 1
e, 男, 2
f, 男, 2

要求查出如下這樣的記錄(每個(gè)班男、女各有幾人):
classId 男 女
1 2 2
2 1 1

請(qǐng)問(wèn)要怎樣寫(xiě)?
如果只是查出每個(gè)班的男生人數(shù)就會(huì):
SELECT class, COUNT(*) AS '男' FROM t_uu WHERE sex='男' GROUP BY class;

大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

reply all(3)
左手右手慢動(dòng)作

Command:

SELECT class_id, SUM(sex = '男') AS male, SUM(sex = '女') AS female FROM student
GROUP BY class_id

Result:

Original data:

select * from student;

洪濤

The data is wrong, isn’t it? If the classid is 2, there should be only 2 boys o(╯□╰)o

黃舟

My answer is based on the original poster’s knowledge, and of course mine too:
First find out the situation of boys, such as t1:

|class_id|male|
|:---:|:---:|
|1|2|
|2|1|
Then find out the girl’s situation, such as t2:

class_id female
1 2
2 1

Finally, chain the two watches together: select t1.class_id,male,female from t1,t2 where t1.class_id = t2.class_id

I haven’t tried it, but it probably means the right thing

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template