MySql-查询并组合不同的条件

MySql-查询并组合不同的条件,mysql,Mysql,实际上,我很难为我的问题找到一个像样的标题 我有一个记录用户何时达到某一特定游戏级别的表格。 级别从1级到5级,但我特别感兴趣的是知道他们在级别3中停留了多长时间(对于任何游戏) 我的表格(gamehistorylog): 我显然对级别低于3的游戏不感兴趣。 我可以使用datediff(),但我不确定如何形成查询,以获得仍处于第3级的学生的组合数据(他们在那里多久了?),以及已经在第4级和第5级通过查询的学生的组合数据(他们在第3级花了多长时间?)? 每次通过关卡时,都会生成一个带有新关卡和特定

实际上,我很难为我的问题找到一个像样的标题

我有一个记录用户何时达到某一特定游戏级别的表格。 级别从1级到5级,但我特别感兴趣的是知道他们在级别3中停留了多长时间(对于任何游戏)

我的表格(gamehistorylog):

我显然对级别低于3的游戏不感兴趣。 我可以使用
datediff()
,但我不确定如何形成查询,以获得仍处于第3级的学生的组合数据(他们在那里多久了?),以及已经在第4级和第5级通过查询的学生的组合数据(他们在第3级花了多长时间?)? 每次通过关卡时,都会生成一个带有新关卡和特定游戏日期的表记录

预期输出只是表上所有处于或高于3级的游戏以及在3级上花费的时间的列表

我有另一个表,它保存着正式的游戏状态,因此我可以加入该表并查询状态为3的游戏的最近日期

select l.gameid as gid,  
DATEDIFF(NOW(), max(dateofchange)) as datediff 
from gamehistorylog l 
join games g on g.gameid = l.gameid 
where g.status = 3 and l.level = 3 ; 

这将获得最高级别为3的所有实例的数据。我不知道如何为那些已经提升到更高水平的人获得它,然后将它们结合起来

您应该显示一些示例数据和预期输出。如果您提供了一些示例输入数据并解释了基于此数据集的逻辑,那么描述您的需求会容易得多。我刚刚添加了一些附加上下文,谢谢。日期差异不应该位于第3级日期的min()和max()上吗?对于具有级别3记录的所有用户?编辑您的问题,并添加一些示例数据行以及这些行的预期输出。
select g.gameid as gid, datediff(curdate(), l.dateofchange) as datediff
from gamehistorylog l
join games g on g.gameid = l.gameid where l.level = 3 and g.status = 3
UNION
select a.gameid as gid, datediff(a.dateofchange, b.dateofchange) as datediff
from gamehistorylog a
join gamehistorylog b on a.gameid = b.gameid
where a.level = 4 and b.level = 3
select g.gameid as gid, datediff(curdate(), l.dateofchange) as datediff
from gamehistorylog l
join games g on g.gameid = l.gameid where l.level = 3 and g.status = 3
UNION
select a.gameid as gid, datediff(a.dateofchange, b.dateofchange) as datediff
from gamehistorylog a
join gamehistorylog b on a.gameid = b.gameid
where a.level = 4 and b.level = 3