【问题标题】:Calculate monthly userscores between two tables using MySQL?使用 MySQL 计算两个表之间的每月用户分数?
【发布时间】:2012-06-18 14:41:50
【问题描述】:

我在stackoverflow上找到了很多有用的信息,但是仍然缺少一点点来解决以下问题:

我有两个表 userscoresuserpoints 像这样:

#table userscores (saves userpoints each month) 

date          userid   points
2012-05-01    1        23
2012-06-01    1        34


#table userpoints (recent points) 

userid   points
1        23
2        10
3        15

我能够找出如何使用以下方法计算当前月份的最近用户点和存储的用户点(来自表“userscores”)之间的差异:

SELECT userpoints.userid, userpoints.points - userscores.points AS mpoints 
    FROM `userpoints`,`userscores` 
    WHERE userpoints.userid=userscores.userid 
        AND YEAR(userscores.date) = YEAR(CURDATE()) 
        AND MONTH(userscores.date) = MONTH(CURDATE())
        AND userpoints.userid != ".$adminID."
    ORDER BY mpoints DESC;"

但是,此查询仅比较两个表中的用户 ID,并忽略表用户点中存在但不存在于表用户分数中的用户 ID。

应修改查询,以便将新创建的用户 ID(在表 userpoints 中)也视为分数。


我发现了如何查询以获取表 usercores 中不存在的用户 ID:

 SELECT userpoints.userid FROM `userpoints` 
      WHERE userpoints.userid 
           NOT IN(SELECT qa_userscores.userid FROM `qa_userscores`)


现在我必须将两者结合起来,但尝试以下方法不起作用:

SELECT userpoints.userid, userpoints.points - userscores.points AS mpoints 
FROM `userpoints`,`userscores` 
WHERE ( 
    userpoints.userid = userscores.userid
    AND YEAR(userscores.date) = YEAR(CURDATE()) 
    AND MONTH(userscores.date) = MONTH(CURDATE())
    AND userpoints.userid != ".$adminID."
)
OR ( 
    userpoints.userid IN(SELECT userpoints.userid FROM `userpoints` 
        WHERE userpoints.userid 
            NOT IN(SELECT DISTINCT userscores.userid FROM `userscores`)) 
)
ORDER BY mpoints DESC;


任何帮助表示赞赏。

【问题讨论】:

    标签: mysql userid


    【解决方案1】:
    SELECT userpoints.userid, userpoints.points - coalesce(userscores.points,0) 
                                                     AS mpoints 
        FROM `userpoints`
        left join `userscores` on userpoints.userid=userscores.userid         
            AND YEAR(userscores.date) = YEAR(CURDATE()) 
            AND MONTH(userscores.date) = MONTH(CURDATE())
        where userpoints.userid != ".$adminID."
        ORDER BY mpoints DESC;"
    

    即使右侧没有对应的行,Left 连接也会自动保留左侧的行。您在右侧检查的所有条件都必须在 Join Condition 中。最后,当右侧值没有时,您需要使用默认值。

    【讨论】:

    • 完美,非常感谢 srini.venigalla!我将在我目前为 www.question2answer.org 编写的插件中感谢您
    • 哦!我会很荣幸,好先生!
    【解决方案2】:

    您可以使用UNION 组合两个查询的结果。

    SELECT userpoints.userid, userpoints.points - userscores.points AS mpoints 
        FROM `userpoints`,`userscores` 
        WHERE userpoints.userid=userscores.userid 
            AND YEAR(userscores.date) = YEAR(CURDATE()) 
            AND MONTH(userscores.date) = MONTH(CURDATE())
    UNION 
    
    SELECT userpoints.userid, points as mpoints FROM `userpoints` 
          WHERE userpoints.userid 
               NOT IN(SELECT userscores.userid FROM `userscores`)
    

    有关UNION 用法的更多信息,请参阅mysql documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-28
      • 2022-01-19
      • 2019-04-12
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多