简体   繁体   English

游戏MySQL表:如何仅计算每个用户的首次尝试并提供正确/不正确的总和?

[英]Game mysql table: How to count only first attempt per user and provide total SUM of correct/incorrect?

I have created a game where users can answer quiz questions - as many times as they want. 我创建了一个游戏,用户可以根据需要多次回答测验问题。 See example question here (stats are in the sidebar top right). 在此处查看示例问题 (统计信息位于侧边栏右上方)。

Currently I am only counting the total statistics, ie all attempts of all users. 目前,我仅统计总数,即所有用户的所有尝试次数。 This, of course, is falsifying the per question statistics. 当然,这是在按问题统计。

SELECT SUM(correct=1) AS correct, SUM(correct=0) AS incorrect, timestamp 
    FROM `gametable` 
    WHERE questionid = #

Example date for the gametable: 游戏桌的日期示例:

+---------------------+--------+------------+---------+
| timestamp           | userid | questionid | correct |
+---------------------+--------+------------+---------+
| 2014-12-07 15:38:35 |      1 |         33 |       0 |
| 2014-12-07 15:39:40 |      1 |         33 |       1 |
| 2014-12-07 15:41:40 |      1 |         33 |       1 |
| 2014-12-07 16:00:17 |      2 |         33 |       1 |
| 2014-12-07 16:08:00 |      2 |         33 |       0 |
| 2014-12-07 16:09:00 |      2 |         33 |       0 |
| 2014-12-07 16:10:25 |      2 |         33 |       1 |
+---------------------+--------+------------+---------+

The result for the above table with the mysql query given would be: 4 correct and 3 incorrect. 给出了mysql查询的上表的结果将是:4个正确和3个不正确。

However, the "first attempt" result should be: 1 correct and 1 incorrect . 但是, “首次尝试”结果应为: 1个正确和1个不正确

Is there a way to write a mysql query to do this? 有没有办法编写一个mysql查询来做到这一点?

Using a sub query to get the first attempt for each user:- 使用子查询获取每个用户的首次尝试:-

SELECT SUM(correct=1) AS correct, SUM(correct=0) AS incorrect
FROM `gametable` 
INNER JOIN
(
    SELECT userid, MIN(timestamp) AS mintimestamp
    FROM `gametable` 
    GROUP BY userid
) sub0
ON gametable.userid = sub0.userid
WHERE questionid = #
AND gametable.timestamp =  sub0.mintimestamp

Note I have removed the timestamp column from your outer select as it didn't appear to be required, nor would it bring back a meaningful value. 注意,我从您的外部选择中删除了timestamp列,因为它似乎不是必需的,也不会带回有意义的值。

Note that your sample data doesn't include a question field, but your example SQL does. 请注意,示例数据不包含问题字段,但示例SQL包含。 Assuming you do have a question field you would need to select the first attempt for the particular question as well:- 假设您确实有一个问题字段,那么您还需要选择特定问题的首次尝试:-

SELECT SUM(correct=1) AS correct, SUM(correct=0) AS incorrect
FROM `gametable` 
INNER JOIN
(
    SELECT userid, questionid, MIN(timestamp) AS mintimestamp
    FROM `gametable` 
    GROUP BY userid, questionid
) sub0
ON gametable.userid = sub0.userid
AND gametable.questionid = sub0.questionid
AND gametable.timestamp =  sub0.mintimestamp
WHERE gametable.questionid = #

or 要么

SELECT SUM(correct=1) AS correct, SUM(correct=0) AS incorrect
FROM `gametable` 
INNER JOIN
(
    SELECT userid, MIN(timestamp) AS mintimestamp
    FROM `gametable` 
    WHERE gametable.questionid = #
    GROUP BY userid
) sub0
ON gametable.userid = sub0.userid
AND gametable.timestamp =  sub0.mintimestamp

EDIT - Suggestion to cope with anonymous users based on a cookie id. 编辑-建议根据Cookie ID处理匿名用户。 This is returning the cookie id if the user id is null, otherwise it is returning null for the cookie id. 如果用户ID为null,则返回cookie id,否则返回cookie id为null。 Done like this so that the cookie id is ignored for records with a user id. 这样做是为了对具有用户ID的记录忽略cookie ID。

SELECT SUM(correct=1) AS correct, SUM(correct=0) AS incorrect
FROM `gametable` 
INNER JOIN
(
    SELECT userid, if(userid IS NULL, cookie_id, NULL) AS cookie_id, questionid, MIN(timestamp) AS mintimestamp
    FROM `gametable` 
    GROUP BY userid, cookie_id, questionid
) sub0
ON ((gametable.userid IS NULL
AND gametable.cookie_id = sub0.cookie_id)
OR (gametable.userid IS NOT NULL
AND gametable.userid = sub0.userid))
AND gametable.questionid = sub0.questionid
AND gametable.timestamp =  sub0.mintimestamp
WHERE gametable.questionid = #

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM