我有一个名为attendance的表,其中包含roll、class_id、status和att_date列。
我有另一个名为class的表,它有class_id和name列。
我想选择不同的class_id和计数number of roll,其中有status = 1 where date="some_date",然后使用inner join.将其连接到类表,然后再次应用where分支=“计算机科学”
但我正面临着一些问题。这是我出席餐桌的一个例子:
roll | class_id | status | att_date
abc | 1 | 0 | 19-06-2016
cvb | 2 | 1 | 19-06-2016
nbs | 1 | 1 | 19-06-2016
lkl | 3 | 1 | 19-06-2016
ewq | 3 | 1 | 19-06-2016
dff | 2 | 1 | 19-06-2016
xyz | 2 | 1 | 19-06-2016这是我的表类的一个例子:
id | name | branch
1 | CS4 | Computer Science
2 | CS5 | Computer Science
3 | CS6 | Mechanical我想要这样的东西:
total number of roll with status 1 | class_id | name
1 | 1 | CS4
3 | 2 | CS5
2 | 3 | CS6有人能解释我吗?
如何处理查询?
发布于 2016-08-20 11:31:43
在group by中使用group by
select cnt, count(*) as num_status_1,
group_concat(a.class_id order by a.class_id) as class_ids,
group_concat(c.name order by a.class_id) as class_names
from (select class_id, count(*) as cnt
from attendance
where status = 1
group by class_id
) a join
class c
on a.class_id = c.class_id
group by cnt;编辑:
注意:这个聚合由cnt进行,您可能不想这样做(您的结果不明确)。这可能就足够了:
select cnt,
a.class_id, c.nameclass_names
from (select class_id, count(*) as cnt
from attendance
where status = 1
group by class_id
) a join
class c
on a.class_id = c.id;甚至:
select c.*,
(select count(*) from attendance a where a.status = 1 and a.class_id = c.id)
from class c;发布于 2016-08-20 11:59:14
我认为这是一种更简单的工作方式:
select a.class_id, b.name, count(a.*) as tot_status_1
from attendance a, class b
where a.class_id=b.id and a.status=1https://stackoverflow.com/questions/39053598
复制相似问题