大体是这样
学生可以选多门课程, 一门课程也可以被多个学生选同时,我这里规定一个老师只能教一门课程 这个是一个一对一的关系学生和课程是一个多对多的关系, 我把他拆分成2个多对一了,用一个中间表 t_kc_info来保存关联关系t_kc_info:
+----+------+------+
| id | sid | kid |
+----+------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 4 |
| 6 | 2 | 5 |
| 7 | 3 | 1 |
| 8 | 1 | 6 |
+----+------+------+其中 sid 为学生的ID,kid为课程的ID现在的问题是
我如何才能查询出,被学生 选学 最多的课程的 名称?课程表为t_kecheng ,字段ID,name 学生表和老师表类似
请教大家了 谢谢
学生可以选多门课程, 一门课程也可以被多个学生选同时,我这里规定一个老师只能教一门课程 这个是一个一对一的关系学生和课程是一个多对多的关系, 我把他拆分成2个多对一了,用一个中间表 t_kc_info来保存关联关系t_kc_info:
+----+------+------+
| id | sid | kid |
+----+------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 4 |
| 6 | 2 | 5 |
| 7 | 3 | 1 |
| 8 | 1 | 6 |
+----+------+------+其中 sid 为学生的ID,kid为课程的ID现在的问题是
我如何才能查询出,被学生 选学 最多的课程的 名称?课程表为t_kecheng ,字段ID,name 学生表和老师表类似
请教大家了 谢谢
from (
select t_kecheng.name,count(*) as cnt
from t_kc_info , t_kecheng
where t_kc_info.kid=t_kecheng.id
group by t_kecheng.name
) t
order by cnt desc limit 1;
from t_kecheng
where (select count(*) from t_kc_info where kid=t_kecheng.id)
= (select max(cnt) from (
select kid,count(*) as cnt
from t_kc_info
group by kid
) t
)
我想以后多请教下SQL 方面的技术问题 这方面我太菜了 谢谢