编辑:
我使用了以下查询:
SELECT emp_name, position, absent,
sum(absent),
(SELECT (absent*100)/sum(absent) FROM employee) AS 'percentage'
FROM employee
而且我只有一排而不是五排。
我通过此视图来查看每个玩家有多少个目标:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `world_cup_2014`.`top_scorer` AS
SELECT
`world_cup_2014`.`players_scored`.`team` AS `team`,
`world_cup_2014`.`players_scored`.`player_name` AS `Player`,
COUNT(`world_cup_2014`.`players_scored`.`player_name`) AS `Number Of Goals`
FROM
`world_cup_2014`.`players_scored`
GROUP BY `world_cup_2014`.`players_scored`.`player_name`
ORDER BY COUNT(`world_cup_2014`.`players_scored`.`player_name`) DESC
它工作正常:
那么,为什么在我的员工表中没有得到类似的结果,而总和呢?
最佳答案
这就是你想要的:
SELECT emp_name, position, absent,
(SELECT SUM(absent) FROM employee) AS 'absence_sum',
((absent*100) / (SELECT SUM(absent) FROM employee)) AS 'percentage'
FROM employee
您需要对缺席的天数使用正式的子查询,以避免RDBMS返回单个结果。
关于mysql - 如何对每一行的所有行求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34036553/