- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个系统,该系统在每天结束时记录用户的总分(“XP”),以便玩家可以随着时间的推移跟踪他们的进度。目前,我正在尝试编写一个查询,该查询可以根据前 X 天的 XP 变化返回排行榜,并带有排名。我不想构建专用的排行榜表,因为间隔 X 可以更改。
编辑
我在这里添加了一个 SQL Fiddle:
http://sqlfiddle.com/#!9/a7c1c/9 - 这是一个没有排名的工作版本 http://sqlfiddle.com/#!9/a7c1c/11 - 这是我能得到的最接近的排名合并(这不起作用,但希望能清楚我正在尝试的内容)
问题:
countsub
子查询中的 HAVING BY
子句的方法;xp_change
列在 countsub
查询中不可用,因此我实际上无法比较更改在我看来,我要么没有正确编写查询,要么遗漏了什么。我一直在尝试找出一种方法来删除 COUNT
子查询,但到目前为止还没有成功。如果有人能指出我正确的方向,那就太好了!
/编辑
这是我的模式:
账户
CREATE TABLE `accounts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`display_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`scanned_at` datetime DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `accounts_display_name_index` (`display_name`),
KEY `accounts_last_tracked_index` (`scanned_at`),
KEY `accounts_slug_index` (`slug`)
)
account_instances
有问题的游戏有多种游戏类型,每种都有不同的排行榜,因此一个帐户可以有多个“实例”:
CREATE TABLE `account_instances` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(10) unsigned NOT NULL,
`game_type_id` int(10) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `account_instances_account_id_game_type_id_unique` (`account_id`,`game_type_id`),
KEY `account_instances_game_type_id_foreign` (`game_type_id`),
CONSTRAINT `account_instances_game_type_id_foreign` FOREIGN KEY (`game_type_id`) REFERENCES `game_types` (`id`)
)
统计数据
这些是用户可以获得 XP 分数的统计数据:
CREATE TABLE `stats` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`display_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `stats_name_unique` (`name`),
UNIQUE KEY `stats_display_name_unique` (`display_name`),
KEY `stats_name_index` (`name`)
)
account_instance_stats
将帐户实例和给定日期的统计数据与分数 (xp
) 相关联:
CREATE TABLE `account_instance_stats` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_instance_id` int(10) unsigned NOT NULL,
`stat_id` int(10) unsigned NOT NULL,
`xp` bigint(20) DEFAULT NULL,
`date` date NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `account_instance_stats_account_instance_id_stat_id_date_unique` (`account_instance_id`,`stat_id`,`date`),
KEY `account_instance_stats_stat_id_foreign` (`stat_id`),
KEY `account_instance_stats_xp_index` (`xp`),
KEY `account_instance_stats_date_index` (`date`),
CONSTRAINT `account_instance_stats_account_instance_id_foreign` FOREIGN KEY (`account_instance_id`) REFERENCES `account_instances` (`id`),
CONSTRAINT `account_instance_stats_stat_id_foreign` FOREIGN KEY (`stat_id`) REFERENCES `stats` (`id`)
)
这是我到目前为止编写的查询。它不会运行,但希望您能看到我在这里尝试的内容:
SELECT
a.*,
SUM(ais.xp - ais2.xp) AS xp_change,
(
select count(*) FROM
(
SELECT COUNT(sub.id)
FROM account_instance_stats AS sub
LEFT JOIN account_instance_stats sub2
ON sub.account_instance_id = sub2.account_instance_id
AND sub.stat_id = sub2.stat_id
AND sub2.date = date_sub(sub.date, INTERVAL 1 day)
JOIN account_instances AS ai ON sub.account_instance_id = ai.id
WHERE ai.game_type_id = 1
AND sub.date = curdate()
AND sub.stat_id = 1
GROUP BY sub.id
HAVING SUM(sub.xp - sub2.xp) > xp_change
) AS countsub
) AS rank
FROM account_instance_stats AS ais
LEFT JOIN account_instance_stats ais2
ON ais.account_instance_id = ais2.account_instance_id
AND ais.stat_id = ais2.stat_id
AND ais2.date = date_sub(ais.date, INTERVAL 1 day)
JOIN account_instances AS ai ON ais.account_instance_id = ai.id
JOIN accounts AS a ON ai.account_id = a.id
WHERE ai.game_type_id = 1
AND ais.date = curdate()
AND ais.stat_id = 1
GROUP BY a.id
ORDER BY rank DESC
LIMIT 10;
父查询的大部分是 account_instance_stats
上的left join
,基于间隔,所以我可以比较 xp
列这两个日期。这一点按预期工作。我正在努力解决的部分是 rank
子查询。这几乎执行相同的查询,但通过计算有多少帐户具有更高的 xp_change
来计算排名。
谢谢!
最佳答案
Here is a solution that uses SQL variables
显示此代码。
SELECT *, @rank := COALESCE(@rank + 1, 1) AS ranking
FROM (
SELECT a.ID AS ID, a.display_name AS display_name
, SUM(ais.xp - ais2.xp) AS xp_change
FROM accounts AS a
JOIN account_instances AS ai
ON ai.account_id = a.id
JOIN account_instance_stats AS ais
ON ais.account_instance_id = ai.id
LEFT JOIN account_instance_stats ais2
ON ais.account_instance_id = ais2.account_instance_id
AND ais.stat_id = ais2.stat_id
AND ais2.date = date_sub(ais.date, INTERVAL 1 day)
WHERE ai.game_type_id = 1
AND ais.date = '2016-06-02'
AND ais.stat_id = 1
GROUP BY a.id
ORDER BY xp_change DESC) AS t1
我将您的表 JOIN 顺序重新排列为 (IMO) 更合乎逻辑的顺序。
鉴于此解决方案:如果您想要单个帐户(假设 ID=2),我会将代码更改为:
SELECT *
FROM (
SELECT *, @rank := COALESCE(@rank + 1, 1) AS ranking
FROM (
SELECT a.ID AS ID, a.display_name AS display_name
, SUM(ais.xp - ais2.xp) AS xp_change
FROM accounts AS a
JOIN account_instances AS ai
ON ai.account_id = a.id
JOIN account_instance_stats AS ais
ON ais.account_instance_id = ai.id
LEFT JOIN account_instance_stats ais2
ON ais.account_instance_id = ais2.account_instance_id
AND ais.stat_id = ais2.stat_id
AND ais2.date = date_sub(ais.date, INTERVAL 1 day)
WHERE ai.game_type_id = 1
AND ais.date = '2016-06-02'
AND ais.stat_id = 1
GROUP BY a.id
ORDER BY xp_change DESC) AS t1
) AS foo
WHERE id = 2;
关于MySQL每日得分随全局排名变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630503/
我的应用程序中有一个 settings.php 页面,它使用 $GLOBALS 来存储网络应用程序中使用的配置。 例如,他是我使用的一个示例设置变量: $GLOBALS["new_login_page
我正在尝试编译我们在 OS 类上获得的简单操作系统代码。它在 Ubuntu 下运行良好,但我想在 OS X 上编译它。我得到的错误是: [compiling] arch/i386/arch/start
我知道distcp无法使用通配符。 但是,我将需要在更改的目录上安排distcp。 (即,仅在星期一等“星期五”目录中复制数据),还从指定目录下的所有项目中复制数据。 是否有某种设计模式可用于编写此类
是否可以在config.groovy中全局定义资源格式(json,xml)的优先级,而不是在每个Resource上指定?例如,不要在@Resource Annotation的参数中指定它,例如: @R
是否有一些简单的方法来获取大对象图的所有关联,而不必“左连接获取”所有关联?我不能只告诉 Hibernate 默认获取 eager 关联吗? 最佳答案 即使有可能有一个全局 lazy=false(谷歌
我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它。与此同时,我正在主线程或子线程中做一些工作...... 我尝试了以下操作,但对话框没有更新...最后一次,
当我偶然发现 this question 时,我正在阅读更改占位符文本。 无论如何,我回去学习了占位符。一个 SO 的回答大致如下: Be careful when designing your pl
例如,如果我有这样的文字: "hello800 more text 1234 and 567" 它应该匹配 1234 和 567,而不是 800(因为它遵循 hello 的 o,这不是一个数字)。 这
我一直在尝试寻找一种无需使用 SMS 验证系统即可验证电话号码(Android 和 iPhone)的方法。原因纯粹是围绕成本。我想要一个免费的解决方案。 我可以安全地假设 Android 操作系统会向
解决此类问题的规范 C++ 设计模式是什么? 我有一些共享多个类的多线程服务器。我需要为大多数类提供各种运行时参数(例如服务器名称、日志记录级别)。 在下面的伪 C++ 代码中,我使用了一个日志记录类
这个问题在这里已经有了答案: Using global variables in a function (25 个答案) 关闭 9 年前。 我是 python 的新手,所以可能有一个简单的答案,但我
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Does C++ call destructors for global and class static
我正在尝试使用 Objective-C 中的 ArrayList 的等价物。我知道我必须使用 NSMutableArray。我想要一个字符串列表 (NSString)。关键是我的列表应该可以从我类(c
今天刚开始学习 Android 开发,我找不到任何关于如何定义 Helper 类或将全局加载的函数集合的信息,我会能够在我创建的任何 Activity 中使用它们。 我的计划是创建(至少目前)2 个几
为什么这段代码有效: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) fun
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven'); $alter = &$GLOBALS
我想知道如何实现一个可以在任何地方使用您自己的设置的全局记录器: 我目前有一个自定义记录器类: class customLogger(logging.Logger): ... 该类位于一个单独的
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。 目前我必须在每个测试文件中包含这个beforeAll: import matchMediaPolyfill from 'm
每次我遇到单例模式或任何静态类(即(几乎)只有静态成员的类)的实现时,我想知道这是否实际上不是一种黑客行为,因此只是为了设计而严重滥用类和实例的原则单个对象,而不是设计类和创建单个实例。对我来说,看起
这个问题在这里已经有了答案: Help understanding global flag in perl (2 个回答) 7年前关闭。 my $test = "There was once an\n
我是一名优秀的程序员,十分优秀!