Mysql - sql如何从第二个表获取最小时间和最大时间记录,其中包括第一个表id

时间:2018-01-11 14:49:28

标签: mysql sql

SELECT c.class_id,c.class_name,c.capacity,c.start_date,c.end_date

FROM `learningcenter_class` as cLEFT JOIN 
( SELECT min(start_time) as min_time, max(end_time) as max_time
 FROM learningcenter_sessions 
 GROUP BY class_id 
)  s ON  s.class_id =c.class_id
WHERE c.start_date between now() and now() + INTERVAL 1 MONTH ORDER BY 
created_date ASC

我有2个表1是类,另一个是会话。我想为这门课提供最短时间和最长时间的课程。我使用的是mysql数据库。这是错误的

1054 - '字段列表'

中的未知列'c.class_id'

1 个答案:

答案 0 :(得分:0)

您没有在内部SQL中选择class_id。将此列添加到选择列表

SELECT class_id , min(start_time) as min_time, max(end_time) as max_time
 FROM learningcenter_sessions 
 GROUP BY class_id