SQL WHERE 乘以 JOIN 和 GROUP BY
SQL WHERE with multiply JOIN and GROUP BY
我正在尝试获得 class 课程安排重叠的房间,我的桌子:课程:
COURSE_ID NAME
11 matematika
22 logika
日程安排:
ID COURSE_ID ID_ROOM DAY HOUR
1 11 105 Mon 10am
2 11 105 Wen 10am
class_room:
ID_ROOM LOCATION CAPACITY
105 A 20
205 B 10
我的 sql 是:
select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h, count(courses.COURSE_ID) as count
from schedule
natural join class_room
natural join courses
group by crid, d, h
order by count desc;
我得到:
crid LOCATION d h count
105 A Mon 10am 3
105 A Thu 10am 2
305 C Mon 11am 1
105 A Wen 10am 1
205 B Wen 10am 1
但我只需要显示计数大于 1 的行。
我试过了
select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h, count(courses.COURSE_ID) as count
from schedule
natural join class_room
natural join courses
where count>1
group by crid, d, h
order by count desc;
但是我收到 1054 错误
如何解决?
不要使用where
。使用 having
select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h, count(courses.COURSE_ID) as count
from schedule
natural join class_room
natural join courses
group by crid, d, h
having count>1
order by count desc;
删除 count > 1
并在组后添加 having count(courses.COURSE_ID) > 1
我正在尝试获得 class 课程安排重叠的房间,我的桌子:课程:
COURSE_ID NAME
11 matematika
22 logika
日程安排:
ID COURSE_ID ID_ROOM DAY HOUR
1 11 105 Mon 10am
2 11 105 Wen 10am
class_room:
ID_ROOM LOCATION CAPACITY
105 A 20
205 B 10
我的 sql 是:
select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h, count(courses.COURSE_ID) as count
from schedule
natural join class_room
natural join courses
group by crid, d, h
order by count desc;
我得到:
crid LOCATION d h count
105 A Mon 10am 3
105 A Thu 10am 2
305 C Mon 11am 1
105 A Wen 10am 1
205 B Wen 10am 1
但我只需要显示计数大于 1 的行。 我试过了
select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h, count(courses.COURSE_ID) as count
from schedule
natural join class_room
natural join courses
where count>1
group by crid, d, h
order by count desc;
但是我收到 1054 错误 如何解决?
不要使用where
。使用 having
select class_room.ID_ROOM as crid, class_room.LOCATION, schedule.DAY as d, schedule.HOUR as h, count(courses.COURSE_ID) as count
from schedule
natural join class_room
natural join courses
group by crid, d, h
having count>1
order by count desc;
删除 count > 1
并在组后添加 having count(courses.COURSE_ID) > 1