简体   繁体   English

告诉3个或更多连续记录缺失的最佳方法

[英]Best way to tell 3 or more consecutive records missing

I'm implementing a achievement system. 我正在实施一个成就系统。 One of the "badges" I'm trying to create will determine: 我正在尝试创建的“徽章”之一将决定:

  1. If a user has joined in at least one coding challenge 如果用户已加入至少一个编码挑战
  2. Then hasn't joined in 3 consecutive coding challenges 然后还没有加入3连续的编码挑战
  3. Then started participating again. 然后又开始参与了。

The badge is simply called "I'll be back" ;-) 徽章简称为“我会回来”;-)

The tables 表格

users
==================
id    fullname
1     Gary Green


challenge
==================================
id  name         start_date
1   challenge1   01-AUG-2010
2   challenge2   03-AUG-2010
3   challenge3   06-SEP-2010
4   challenge4   07-SEP-2010
5   challenge5   30-OCT-2010
6   challenge6   05-NOV-2010


entries
====================================================
id   challengeid    userid    type       code
1     1             1         1          -
2     2             1         1          -
3     6             1         1          -
4     6             1         2          -

The "type" in the entries table refers to if the entry type is either a non-regex based entry or regex based one. 条目表中的“类型”是指条目类型是基于非正则表达式的条目还是基于正则表达式的条目。 A user can submit both a regex and non-regex entry, therefore the above entry for challenge 6 is valid. 用户可以提交正则表达式和非正则表达式条目,因此挑战6的上述条目是有效的。

Example output 示例输出

This is the style output of the query I would like (in this case the badge should be awarded): 这是我想要的查询的样式输出(在这种情况下应该授予徽章):

(for userid 1)
Challenge 1 --> Joined in
Challenge 2 --> Joined in
Challenge 3 --> NULL
Challenge 4 --> NULL
Challenge 5 --> NULL
Challenge 6 --> Joined in

How? 怎么样?

Here are my questions 这是我的问题

  1. Whats the best way to do this in a query? 什么是在查询中执行此操作的最佳方法?
  2. Is there a function I can use in MySQL to SELECT this range without resorting to some PHP? 有没有我可以在MySQL中使用的函数来选择这个范围而不需要使用某些PHP?

The query so far 到目前为止的查询

I'm doing a LEFT OUTER JOIN to join the challenge table and entries table (LEFT OUTER to make sure to preserve the challenges the user has not joined in), then sort by challenge start_date to see if the user has not joined in for 3 or more consecutive challenges. 我正在进行LEFT OUTER JOIN以加入挑战表和条目表(LEFT OUTER以确保保留用户未加入的挑战),然后按挑战start_date排序以查看用户是否尚未加入3或更多连续挑战。

SELECT challenge.id AS challenge_id, entries.id AS entry_id
FROM challenge_entries entries
LEFT OUTER JOIN challenge_details challenge
 ON entries.challengeid = challenge.id
WHERE entries.userid = <user_id>
ORDER BY challenge.start_date
GROUP BY entries.challengeid

important edit: for this badge to make sense the criteria will need to be 3 or more consecutive challenges sandwiched between challenges that were joined in ie like the example output above. 重要的编辑:要使这个徽章有意义,标准将需要连接3个或更多连续挑战,这些挑战被加入到了上面的示例输出中。 Otherwise anyone who joins in a challenge for the first time will automatically receive the badge. 否则,任何第一次参加挑战的人都将自动收到徽章。 The user has to be seen to have been "away" from participating in challenges for a while (>=3) 必须让用户远离参与挑战一段时间(> = 3)

I think you need to use the other table to start from... 我认为你需要使用另一个表开始......

SELECT challenge.id AS challenge_id, entries.id AS entry_id FROM challenge_details challenge
LEFT JOIN challenge_entries entries ON entries.challengeid = challenge.id and entries.userid = <user_id>
ORDER BY challenge.start_date

adding a group by can be done as you want... 添加组可以按照您的意愿完成...

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

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