基本上问题是如何从#hours获得总和,通过#id,#code,#semester,#thisar columns来填充
id, hours, code, semester, year
165, 6, 3, 1, 2012
166, 6, 16, 1, 2012
460, 8, 14, 2, 2012
461, 8, 16, 2, 2012
462, 15, 16, 2, 2013
463, 6, 1, 2, 2013
id, hours, code, semester, year, sum
165, 6, 3, 1, 2012, **6**
166, 6, 16, 1, 2012, **6**
460, 8, 14, 2, 2012, **8**
461, 8, 16, 2, 2012, **23**
462, 15, 16, 2, 2012, **23**
463, 6, 1, 2, 2013, **6**
我已经尝试过GROUP BY,GROUP BY CONCAT等,但没有成功。
我希望按行中的所有这些列进行分组
答案 0 :(得分:1)
试试这个:
select id, code, semester, year, sum(hours) from `table` group by id,code,semester, year
答案 1 :(得分:0)
要对可以使用MYSQL的SUM()
函数的列的值求和,您可以将其作为此类字段返回,例如此查询中的求和字段将为total_hours
SELECT id,hours,code,semester,year, SUM(hours) AS total_hours
FROM table_name
GROUP BY id,code,semester, year