MySQL Right Outer加入Null Value,排名(排名)与所有用户

时间:2015-09-11 16:51:12

标签: mysql

我试图创建排名(排名)查询。

我使用表usersschedulepicks来计算排名。然而,如果用户没有提交用户不会在积分榜中显示的任何选秀权,那么这个缺陷就会存在。

以下查询将返回已提交选择的所有用户。用户2和用户4尚未提交选秀权,并且没有显示。

+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
| userID | userName | totalWins | totalLost | totalPushs | totalPoints | totalBets | trueBets |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   1    |   aaaa   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   3    |   cccc   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   5    |   eeee   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+

我试图创建它,所有用户都会出现在积分榜上,无论他们是否提交了选秀权。如下所示。

+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
| userID | userName | totalWins | totalLost | totalPushs | totalPoints | totalBets | trueBets |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   1    |   aaaa   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   2    |   bbbb   |     0     |     0     |     0      |      0      |     0     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   3    |   cccc   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   4    |   dddd   |     0     |     0     |     0      |      0      |     0     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   5    |   eeee   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+

然而,我收到了这个

+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
| userID | userName | totalWins | totalLost | totalPushs | totalPoints | totalBets | trueBets |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   1    |   aaaa   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|  NULL  |   NULL   |     0     |     0     |     0      |      0      |     0     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   3    |   cccc   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|  NULL  |   NULL   |     0     |     0     |     0      |      0      |     0     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+
|   5    |   eeee   |     0     |     0     |     1      |     0.5     |     1     |    0     |
+--------+----------+-----------+-----------+------------+-------------+-----------+----------+

我已经到了正在尝试RIGHT OUTER JOIN users表的地方,其中所有用户都驻留在查询中,但由于没有匹配,NULL值显示为预期。

我如何能够使用正确的userIDuserName

替换这些NULL值

这是我的代码

SELECT
    lt.userID,
    lt.userName,
    IFNULL(lt.totalWins,0) AS totalWins,
    IFNULL(lt.totalLost,0) AS totalLost,
    IFNULL(lt.totalPushs,0) AS totalPushs,
    IFNULL((totalWins+(totalPushs*.5)),0) AS totalPoints,
    IFNULL(totalWins+totalLost+totalPushs,0) AS totalBets,
    IFNULL(totalWins+totalLost,0) AS trueBets
FROM (
    SELECT
        userID,
        userName,
        SUM(win) AS totalWins,
        SUM(lost) AS totalLost,
        SUM(push) AS totalPushs
    FROM (
        SELECT
            *,
            (finalResult = 'win') AS win,
            (finalResult = 'loss') AS lost,
            (finalResult = 'push') AS push
        FROM (
            SELECT 
                users.userID,
                userName,
                IF (pickID=visitorID, visitorResult, homeResult) AS finalResult
            FROM
                users
            JOIN
                picks 
            ON
                users.userID = picks.userID
            JOIN
                schedule
            ON
                picks.gameID = schedule.gameID
            WHERE
                weekNum <= 1
        ) x
    ) x
    GROUP BY
        userID
) lt
RIGHT OUTER JOIN
    users
ON
    users.userID = lt.userID
LIMIT 100

2 个答案:

答案 0 :(得分:0)

你可以尝试:

SELECT
    lt.userID,
    lt.userName,
    IFNULL(lt.totalWins,0) AS totalWins,
    IFNULL(lt.totalLost,0) AS totalLost,
    IFNULL(lt.totalPushs,0) AS totalPushs,
    IFNULL((totalWins+(totalPushs*.5)),0) AS totalPoints,
    IFNULL(totalWins+totalLost+totalPushs,0) AS totalBets,
    IFNULL(totalWins+totalLost,0) AS trueBets
FROM users
RIGHT OUTER JOIN
    (
    SELECT
        userID,
        userName,
        SUM(win) AS totalWins,
        SUM(lost) AS totalLost,
        SUM(push) AS totalPushs
    FROM (
        SELECT
            *,
            (finalResult = 'win') AS win,
            (finalResult = 'loss') AS lost,
            (finalResult = 'push') AS push
        FROM (
            SELECT 
                users.userID,
                userName,
                IF (pickID=visitorID, visitorResult, homeResult) AS finalResult
            FROM
                users
            JOIN
                picks 
            ON
                users.userID = picks.userID
            JOIN
                schedule
            ON
                picks.gameID = schedule.gameID
            WHERE
                weekNum <= 1
        ) x
    ) x
    GROUP BY
        userID
) lt
ON
    users.userID = lt.userID
LIMIT 100

我没有测试过这个,但主要的变化是从users表开始,然后加入你的结果。您之前的问题是因为您从结果开始并尝试加入users,并且在用户未进行任何选择的情况下,它们将返回null。这种方式应该导致结果为null,但显示所有用户作为起点,所以你将拉出所有记录

答案 1 :(得分:0)

提出Doug's建议,以users表开头并重新编写代码。

终于得到了我需要的东西。

而不是JOIN我使用LEFT JOIN并将WHERE weekNum <= 1更改为WHERE (weekNum <= 1 OR weekNum IS NULL)

以下代码

SELECT
    *,
    (totalWins+(totalPushs*.5)) AS totalPoints,
    totalWins+totalLost+totalPushs AS totalBets,
    totalWins+totalLost AS trueBets
FROM (
    SELECT
        userID,
        userName,
        IFNULL(SUM(win),0) AS totalWins,
        IFNULL(SUM(lost),0) AS totalLost,
        IFNULL(SUM(push),0) AS totalPushs
    FROM (
        SELECT
            *,
            (finalResult = 'win') AS win,
            (finalResult = 'loss') AS lost,
            (finalResult = 'push') AS push
        FROM (
            SELECT 
                u.userID,
                userName,
                IF (pickID=visitorID, visitorResult, homeResult) AS finalResult
            FROM
                users AS u
            LEFT JOIN
                picks AS p
            ON
                u.userID = p.userID
            LEFT JOIN
                schedule AS s
            ON
                p.gameID = s.gameID
            WHERE
                (weekNum <= 1 OR weekNum IS NULL)
        ) x
    ) x
    GROUP BY
        userID
) x
ORDER BY
    totalPoints DESC