请教一个 sql 怎么写

2020-08-07 14:48:36 +08:00
 coolair

表结构如下:

category state target_id
a01
a11
b11
b21
c31
d22

想要的结果是: target_id 为 1 且 state>0 的 category 类别的的个数,比如,以上只有 a 、b 、c 符合,所以,结果是 3 。

1635 次点击
所在节点    问与答
15 条回复
lxk11153
2020-08-07 14:50:19 +08:00
233 不支持表格,你可以学我这样子: /t/696035
coolair
2020-08-07 14:54:11 +08:00
@lxk11153 #1 表格搞定,帮忙看看,多谢。
DelayNoMay
2020-08-07 14:54:18 +08:00
用 group by category,再按条件 count 一下
whisper3032
2020-08-07 14:54:37 +08:00
select count(distinct category) from table where target_id = 1 and state > 0
coolair
2020-08-07 14:55:02 +08:00
搞表格搞了半天,现在主题不可编辑了,上面写错了,结果应该是只有 b 、c 符合,结果是 2.
coolair
2020-08-07 14:56:20 +08:00
@lxk11153 #1 顺带告诉下你,表格可以直接在 markdown 语法下写<table><tr><td></td></tr></table>
lxk11153
2020-08-07 14:56:52 +08:00
@coolair #2 怎么搞的? markdown+html(table)吗?
lxk11153
2020-08-07 14:57:37 +08:00
@coolair #6 呵呵,我已经猜到。[doge]。同样表示感谢
coolair
2020-08-07 14:57:42 +08:00
@whisper3032 #4
@DelayNoMay #3 不好意思,前面表达有点错误,应该是同一分类所有 state>0 。
DelayNoMay
2020-08-07 14:58:36 +08:00
select count(1) from table where state>0 and target_id = 1 group by category
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;
flowfire
2020-08-07 15:08:50 +08:00
不是 group by 吗。。。。子查询里 group by,再 count 计数
但是感觉这么搞性能方面很堪忧
shine20070522
2020-08-07 15:21:26 +08:00
group by category having min(state) > 0
coolair
2020-08-07 15:28:39 +08:00
@shine20070522 #13 感谢老哥!
zhuangjia
2020-08-07 16:04:35 +08:00
考试场景:查找所有科目成绩都及格的同学人数

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/696466

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX