在左连接结果之后,每个用户需要一个反结果

after left-join result, need a countresult per user

我已经成功地创建了一个查询,甚至有效。

SELECT voterID,vote,nick as player
FROM 
trust LEFT JOIN players
ON trust.playerID=players.playerID
ORDER BY trust.vote DESC, trust.playerID DESC

这给了我所有选票的完整列表,但我想对每个玩家的选票求和,所以我得到每个玩家 1 行,信任点总数为 1。(可以是正数或负数。+1 或-1 每个投票者,每个选民)

table信任:

CREATE TABLE `trust` (
  `rowID` int(10) UNSIGNED NOT NULL,
  `playerID` int(11) UNSIGNED NOT NULL,
  `voterID` int(11) UNSIGNED NOT NULL,
  `vote` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



INSERT INTO `trust` (`rowID`, `playerID`, `voterID`, `vote`) VALUES
(25, 2187, 1, 1),
(26, 23193, 1, 1),
(27, 2050, 1, 1),
(29, 3714, 1, 1),
(31, 1, 2187, 1),
(32, 30363, 29937, 1),
(33, 15837, 26102, 1),
(34, 30058, 26102, 1),
(35, 30539, 26102, -1),
(36, 28382, 26102, -1),
(37, 18692, 26102, 1),
(38, 6440, 14143, 1),
(39, 15069, 8306, 1),
(40, 2050, 2187, 1),
(41, 3233, 1, 1),
(42, 12664, 26102, 1),
(43, 30539, 2187, -1),
(44, 28382, 2187, -1),
(45, 30539, 1, -1),
(46, 10138, 1, 1);

预期结果:姓名列表和总票数SUM

您可以加​​入player table 合计查询票数:

SELECT   nick AS player, sum_votes
FROM     players p
JOIN     (SELECT   playerid, SUM(vote) AS sum_votes
          FROM     trust
          GROUP BY playerid) t ON p.playerid = t.playerid
ORDER BY 2 DESC