首页 > 解决方案 > The Having statement does not work in my sql

问题描述

Try to write a query like this:

select distinct S.course_id,count(S.course_id)
from Section As S
Where Exists (Select S.course_id
             From Section 
             Where S.year='2019'
             Having count(S.course_id)>1 )
Group by S.course_id
Order by S.course_id DESC;

But the result countinue to give me course_ids that does not appear more than once.

course_id count(S.course_id)
PHY-101 1
EE-181 1
CMPE-347 1
CMPE-190 2
CMPE-101 1
BIO-101 1

enter image description here

标签: mysqlsql

解决方案


尝试修改您的查询如下

select s.course_id, count(s.course_id)
from Section as s
Where Exists (Select * From Section s2
             Where s2.year='2019' and s2.course_id=s.course_id
             group by s2.course_id
             Having count(*)>1)
Group by s.course_id
Order by s.course_id DESC;

推荐阅读