我需要从tblgrade中选择grade_name,从tblsubject中选择subject_name,从tblsubcription中选择count(subscribe_id),从sub_status=1的tblsubcription中选择count(sub_status),从sub_status为空的tblsubcription中选择count(sub_status)。
这就是我所尝试的:
SELECT t2.grade_name,
t.subject_name,
(SELECT COUNT(*)
FROM tblsubcription
WHERE sub_status IS NULL
AND teacher_id = 2) AS pending,
(SELECT COUNT(*)
FROM tblsubcription
WHERE sub_status = '1'
AND teacher_id = 2) AS appoved,
COUNT(t1.subscribe_id) AS totalsub
FROM tblsubject t
INNER JOIN tblsubject_grade tg ON (t.subject_id = tg.subject_id)
INNER JOIN tblsubcription t1 ON (tg.subject_garde_id = t1.subject_garde_id)
INNER JOIN tblgrade t2 ON (tg.grade_id = t2.grade_id)
AND tg.grade_id = t2.grade_id
AND tg.subject_id = t.subject_id
AND t2.admin_id = t.admin_id
WHERE t1.teacher_id = 2
GROUP BY t.subject_name,
t2.grade_name;
查看执行上述查询时得到的结果,我需要的预期结果为红色
1条答案
按热度按时间nbewdwxp1#
查看以下子查询:
此处没有任何内容可以将其与特定行关联。您需要在
WHERE
子句中添加一个条件,以告诉您要查看哪个级别/科目对。另一个(已批准)子查询也是如此。或者,您也可以使用
tblsubscription
和conditional aggregation的另一个联接来解决此问题。我想发布代码来解决这个问题,但是我发现图片太模糊了,读起来不太清楚,所以我不能很容易地推断出应该使用哪些字段。下次发布格式化文本,你会在更短的时间内得到更好的答案。