我有以下代码,其中,我根据用户获得练习的ID和练习的总时间。
SELECT practice.id, SUM(practice.duration) as dur
FROM trainingPractice
INNER JOIN training
ON trainingPractice.trainingId = training.id
INNER JOIN trainingStudent
ON trainingPractice.trainingId = trainingStudent.trainingId
INNER JOIN practice
ON trainingPractice.practiceId = practice.id
WHERE trainingStudent.studentId = '$id'
GROUP BY practice.id
但是,我是否有可能获得该练习的总时间?当然在同一个查询中。
示例:
id | dur | durTotal |
-------------------------------
23 45 240
用户在240分钟内练习23分钟45分钟
答案 0 :(得分:1)
只需添加另一个子查询。
Marshal.load
答案 1 :(得分:1)
我不确定这是否会完全正常,因为我不知道您的确切表结构和关系,我也更习惯MS SQL Server而不是MySQL,但它应该指向正确的方向:
SELECT practice.id,
SUM(practice.duration) as durTotal,
SUM(CASE trainingStudent.studentId WHEN '$id' THEN practice.duration ELSE 0 END) as dur
FROM trainingPractice
INNER JOIN training
ON trainingPractice.trainingId = training.id
INNER JOIN trainingStudent
ON trainingPractice.trainingId = trainingStudent.trainingId
INNER JOIN practice
ON trainingPractice.practiceId = practice.id
GROUP BY practice.id