mysql - SQL WHERE 与乘法 JOIN 和 GROUP BY

标签 mysql sql join count group-by

我正在尝试获得类(class)表重叠的教室,我的表格:类(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

教室:

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。使用拥有

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;

关于mysql - SQL WHERE 与乘法 JOIN 和 GROUP BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28520639/

相关文章:

php - 在自动填充的 PHP 表之间放置文本

mysql - 在MySQL中,有没有某种方法可以用数字列表计算平均值?

javascript - 使用选择下拉列表调用 javascript 函数

sql - 如何插入显式值和从另一个表检索的数据的混合

php - 来自数据库的发布日期

SQL "transform"查询

mysql - 连接多个表并使用 MySQL 的 Count

sql - 如何在SQL中为表中的每一行随机输入DOB?

mongodb - 为什么 MongoDB 文档建议不要使用 DBREF?

SQL:连接两个表,其中一个表具有不同的行