COUNT,每个课程的最小值没有子查询

时间:2013-12-04 11:46:09

标签: mysql

任何人都可以帮我找到每门课程的最低学生行为吗?列出如下:

+-------------+--------------------------+  
| Course      | Lowest Action            |  
+-------------+--------------------------+ 
| Maths Y1    |                          |  
| English C   |                          |   
| Science Y1  |                          |  

对于所有用户,即使他们不在日志表中,没有子查询?感谢@luckylwk对我初步查询的帮助。我有一个带有子查询的解决方案,但是想把它放到一个变量中以进行大量查询。

SELECT 
    COUNT(tbl_log.action)
    lastname,
    c.fullname,
FROM tbl_log
JOIN tbl_user ON tbl_log.userid = tbl_user.id
JOIN tbl_course ON tbl_log.course = tbl_course.id
GROUP BY tbl_log.userid, tbl_log.course

LOG TABLE     

+-------------+---------------------+------+-----+---------+----------------+  
| Field       | Type                | Null | Key | Default | Extra          |  
+-------------+---------------------+------+-----+---------+----------------+  
| id          |                     | NO   | PRI | NULL    | auto_increment |  
| time        |                     | NO   |     | NULL    |                |  
| userid      |                     | NO   |     | NULL    |                |  
| course      |                     | NO   |     | NULL    |                |  
| action      |                     | NO   |     | NULL    |                |  
+-------------+---------------------+------+-----+---------+----------------+  

USER Table

+--------------+---------------------+------+-----+---------+----------------+  
| Field        | Type                | Null | Key | Default | Extra          |  
+--------------+---------------------+------+-----+---------+----------------+  
| id           |                     | NO   | PRI | NULL    | auto_increment |  
| username     |                     | NO   |     | NULL    |                |  
| userpassword |                     | NO   |     | NULL    |                |  
| lastname     |                     | NO   |     | NULL    |                |  
| firstname    |                     | NO   |     | NULL    |                |  
+--------------+---------------------+------+-----+---------+----------------+  

COURSE table

+--------------+---------------------+------+-----+---------+----------------+  
| Field        | Type                | Null | Key | Default | Extra          |  
+--------------+---------------------+------+-----+---------+----------------+  
| id           |                     | NO   | PRI | NULL    | auto_increment |  
| category     |                     | NO   |     | NULL    |                |  
| fullname     |                     | NO   |     | NULL    |                |  
| shortname    |                     | NO   |     | NULL    |                |   
+--------------+---------------------+------+-----+---------+----------------+  

我通过注册和上下文表将用户链接在一起。

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT 
 tbl_course.course_name,
 MIN(tbl_log.action) as Lowest_Action
 FROM tbl_log
 JOIN tbl_user ON tbl_log.userid = tbl_user.id
 JOIN tbl_course ON tbl_log.course = tbl_course.id
 GROUP BY tbl_course.course_name

参见 Fiddle Demo