SQL Sum函数限制0,3返回整列的总和

时间:2014-05-09 06:42:14

标签: php mysql sql sum

下午好,

我希望在以下查询中找到总列的总和:

SELECT * 
FROM  `btb_picks` 
WHERE  `player_name` =  'bryan'
ORDER BY  `total` 
LIMIT 0 , 3

该表共有9条记录,其中player_name =' bryan'

Position Golfer Name    Round 1 Round 2 Round 3 Round 4 Today   Through Total   Player Name
T4       Lee Westwood   67      12:25   -       -       -5      F       -5      bryan
T4       Justin Rose    67      1:39    -       -       -5      F       -5      bryan
T4       Sergio Garcia  67      1:49    -       -       -5      F       -5      bryan

我想收集这3条记录的总数(-15)之和

当我运行以下查询时:

SELECT SUM( total ) AS 3_man
FROM  `btb_picks` 
WHERE  `player_name` =  'bryan'
ORDER BY total
LIMIT 0 , 3

它返回:

3_man -13

这是整个总列的总和,而不是我有兴趣找到总数的3条记录。

当我以这种方式尝试查询时:

SELECT * FROM `btb_picks` WHERE `player_name` in ( SELECT * 
    FROM  `btb_picks` 
    WHERE  `player_name` =  'bryan'
    ORDER BY  `total` 
    LIMIT 0 , 3 );

SQL咆哮:

1235 - 这个版本的MySQL还没有支持' LIMIT& IN / ALL / ANY / SOME子查询'

如何获得这三个记录的总和?

1 个答案:

答案 0 :(得分:0)

试试这个......

SELECT SUM(s.score) FROM
(SELECT total as score FROM btb_picks WHERE player_name = 'bryan' ORDER BY total LIMIT 0,3) s