2

此表存储用户匹配之间的用户投票。总是有一个赢家,一个输家和选民。

CREATE TABLE `user_versus` (
  `id_user_versus` int(11) NOT NULL AUTO_INCREMENT,
  `id_user_winner` int(10) unsigned NOT NULL,
  `id_user_loser` int(10) unsigned NOT NULL,
  `id_user` int(10) unsigned NOT NULL,
  `date_versus` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_user_versus`),
  KEY `id_user_winner` (`id_user_winner`,`id_user_loser`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;

INSERT INTO `user_versus` (`id_user_versus`, `id_user_winner`, `id_user_loser`, `id_user`, `date_versus`) VALUES
(1, 6, 7, 1, '2013-10-25 23:02:57'),
(2, 6, 8, 1, '2013-10-25 23:02:57'),
(3, 6, 9, 1, '2013-10-25 23:03:04'),
(4, 6, 10, 1, '2013-10-25 23:03:04'),
(5, 6, 11, 1, '2013-10-25 23:03:10'),
(6, 6, 12, 1, '2013-10-25 23:03:10'),
(7, 6, 13, 1, '2013-10-25 23:03:18'),
(8, 6, 14, 1, '2013-10-25 23:03:18'),
(9, 7, 6, 2, '2013-10-26 04:02:57'),
(10, 8, 6, 2, '2013-10-26 04:02:57'),
(11, 9, 8, 2, '2013-10-26 04:03:04'),
(12, 9, 10, 2, '2013-10-26 04:03:04'),
(13, 9, 11, 2, '2013-10-26 04:03:10'),
(14, 9, 12, 2, '2013-10-26 04:03:10'),
(15, 9, 13, 2, '2013-10-26 04:03:18'),
(16, 9, 14, 2, '2013-10-26 04:03:18');

我正在处理一个获取类似配置文件的查询。配置文件类似,当投票百分比(赢与输)为指定配置文件的 +/- 10% 时。

SELECT id_user_winner AS id_user,
    IFNULL(wins, 0) AS wins,
    IFNULL(loses, 0) AS loses,
    IFNULL(wins, 0) + IFNULL(loses, 0) AS total,
    IFNULL(wins, 0) / (IFNULL(wins, 0) + IFNULL(loses, 0)) AS percent
FROM
(
    SELECT id_user_winner AS id_user FROM user_versus 
    UNION
    SELECT id_user_loser FROM user_versus 
) AS u
LEFT JOIN
(
    SELECT id_user_winner, COUNT(*) AS wins
    FROM user_versus
    GROUP BY id_user_winner
) AS w
ON u.id_user = id_user_winner
LEFT JOIN
(
    SELECT id_user_loser, COUNT(*) AS loses
    FROM user_versus
    GROUP BY id_user_loser
) AS l
ON u.id_user = l.id_user_loser

这是当前的结果:

mysql结果

它当前返回 NULL 行,它们不应该在那里。仍然需要优化(并且不能完全放在我的手指上)是:

  1. 只带来与用户 ABC 相似的用户
  2. 指定定义谁是相似用户的条件,例如用户 id = 6(其中相似用户与用户 id 6 的百分比差异为 +/- 10%)

任何帮助将不胜感激。谢谢!

4

1 回答 1

1

要计算每个用户的胜负,而不必将表连接到自身并使用 OUTER 连接,可以单独选择胜负并在它们之间执行 UNION ALL,但如果给定行表示获胜,则使用附加信息用户,还是一个损失。

然后,很容易计算每个用户的所有输赢。棘手的部分是合并选项以指定您想与哪个用户比较配置文件。我使用一个变量来做到这一点,该变量设置为percentage给定用户的值user_id,您可以将其从常量更改为变量。

这是我的建议(与 id = 6 的用户相比):

SELECT
    player_id AS id_user,
    wins,
    losses,
    wins + losses AS total,
    wins / (wins + losses) AS percent
  FROM (
    SELECT
        player_id,
        SUM(is_a_win) wins,
        SUM(is_a_loss) losses,
        CASE
          WHEN player_id = 6
            THEN @the_user_score := SUM(is_a_win) / (SUM(is_a_win) + SUM(is_a_loss))
          ELSE NULL
         END
      FROM (
        SELECT id_user_winner AS player_id, 1 AS is_a_win, 0 AS is_a_loss FROM user_versus
        UNION ALL SELECT id_user_loser, 0, 1 FROM user_versus
      ) games
    GROUP BY player_id
  ) data
WHERE
  ABS(wins / (wins + losses) - @the_user_score) <= 0.1
;

输出:

ID_USER 赢得损失总百分比
6 8 2 10 0.8
9 6 1 7 0.8571

您当然可以通过在最外面的子句中添加player_id != 6(或者,在最终解决方案中,一些变量名)条件来删除其个人资料作为比较基础的用户。WHERE

SQLFiddle 上的示例:匹配配置文件 - 示例

如果这是您正在寻找的内容,您能否提供一些反馈?如果不是,您希望得到什么输出?

于 2013-11-08T23:28:08.153 回答