计算GROUP_CONCAT内的值

时间:2015-08-23 18:18:44

标签: php mysql

有没有办法计算GROUP_CONCAT内的值?

例如,这是我的查询:

$realIP = file_get_contents("http://ipecho.net/plain");

给出了这个输出:

enter image description here

我希望SELECT users.*, GROUP_CONCAT(written.score separator ' - ') as Wscore, student_subject.* FROM users JOIN written ON users.idnumber=written.idnumber JOIN student_subject ON users.idnumber=student_subject.idnumber WHERE student_subject.teacher='$login_session' AND student_subject.section='$section' AND student_subject.level='$level' AND student_subject.year='$year' AND student_subject.subject='$subject' GROUP BY users.idnumber 列填充TOTAL值的总和。

表'表'看起来像:

enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的查询:

SELECT users.*,
       GROUP_CONCAT(written.score separator ' - ') as Wscore,
       student_subject.*,
       SUM(written.score) as total 
FROM users
  JOIN written ON users.idnumber=written.idnumber
  JOIN student_subject ON users.idnumber=student_subject.idnumber
WHERE
      student_subject.teacher='$login_session'
  AND student_subject.section='$section'
  AND student_subject.level='$level'
  AND student_subject.year='$year'
  AND student_subject.subject='$subject'
GROUP BY users.idnumber

关键更改是将SUM(written.score) as total添加到选择列表中。它产生了正确的结果:

enter image description here