将此子查询转换为JOIN?

时间:2013-12-03 17:12:01

标签: mysql join count subquery

有谁知道是否可以将此子查询转换为JOIN?

SELECT DISTINCT

lastname,
c.fullname,

(SELECT COUNT(lg.action) FROM tbl_log AS lg WHERE lg.userid = u.id AND lg.course = c.id) AS 'Total Course Hits Per Student'

FROM tbl_user AS u    
JOIN tbl_user_enrolments AS ents ON ents.userid = u.id    
JOIN tbl_enrol AS en ON ents.enrolid = en.id    
JOIN tbl_course AS C ON c.id = en.courseid    
JOIN tbl_context AS ctx ON c.id = ctx.instanceid    
JOIN tbl_role_assignments AS ra ON ra.contextid = ctx.id AND ra.userid = u.id       

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)

你不能对log_table进行查询

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

我不知道你是否需要其他桌子?如果我没有记错的话,这会提供一个计数,学生姓名和课程名称。但是,您只能获得实际日志,因此没有任何事情的学生不会出现。

否则OUTER JOIN可能就足够了。我现在没时间检查SQL小提琴了。希望这已经对你有所帮助了。