表结构如下:
category | state | target_id |
a | 0 | 1 |
a | 1 | 1 |
b | 1 | 1 |
b | 2 | 1 |
c | 3 | 1 |
d | 2 | 2 |
想要的结果是: target_id 为 1 且 state>0 的 category 类别的的个数,比如,以上只有 a 、b 、c 符合,所以,结果是 3 。
3
DelayNoMay 2020-08-07 14:54:18 +08:00
用 group by category,再按条件 count 一下
|
4
whisper3032 2020-08-07 14:54:37 +08:00
select count(distinct category) from table where target_id = 1 and state > 0
|
5
coolair OP 搞表格搞了半天,现在主题不可编辑了,上面写错了,结果应该是只有 b 、c 符合,结果是 2.
|
6
coolair OP @lxk11153 #1 顺带告诉下你,表格可以直接在 markdown 语法下写<table><tr><td></td></tr></table>
|
9
coolair OP |
10
DelayNoMay 2020-08-07 14:58:36 +08:00
select count(1) from table where state>0 and target_id = 1 group by category
|
11
butterf1y 2020-08-07 14:59:00 +08:00
SELECT count(*) FROM(SELECT DISTINCT category from t_test WHERE target_id = 1 AND state > 0)as temp;
|
12
flowfire 2020-08-07 15:08:50 +08:00
不是 group by 吗。。。。子查询里 group by,再 count 计数
但是感觉这么搞性能方面很堪忧 |
13
shine20070522 2020-08-07 15:21:26 +08:00 4
group by category having min(state) > 0
|
14
coolair OP @shine20070522 #13 感谢老哥!
|
15
zhuangjia 2020-08-07 16:04:35 +08:00
考试场景:查找所有科目成绩都及格的同学人数
|