如何对每行中的所有行求和

时间:2015-12-02 06:48:35

标签: mysql

修改

我使用了这个查询:

SELECT emp_name, position, absent, 
sum(absent), 
(SELECT (absent*100)/sum(absent) FROM employee) AS 'percentage' 
FROM employee

我只有一行而不是5行。

我提出这个观点,看看每个球员有多少目标:

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

它运作良好:

那么为什么在我的员工表中我没有得到类似的结果,总结是什么?

2 个答案:

答案 0 :(得分:1)

这就是你想要的:

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返回单个结果。

答案 1 :(得分:0)

使用此:

$pattern = '/[$ch]/';