我有两张桌子,记录:
CREATE TABLE IF NOT EXISTS `records` (
`recordid` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`platform` varchar(255) NOT NULL,
`track` varchar(255) NOT NULL,
`trackid` int(11) NOT NULL,
`bike` varchar(255) NOT NULL,
`time` decimal(7,3) NOT NULL,
`faults` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`verified` varchar(3) NOT NULL DEFAULT 'no',
`wr` varchar(3) NOT NULL DEFAULT 'no',
PRIMARY KEY (`recordid`),
KEY `userid` (`userid`)
)
和用户:
CREATE TABLE IF NOT EXISTS `users` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`country` varchar(50) NOT NULL,
`verified` varchar(3) NOT NULL DEFAULT 'no',
PRIMARY KEY (`userid`)
)
我存储了一些示例数据:
+----------+--------+----------+----------------+---------+-----------+---------+--------+---------------------+----------+-----+
| recordid | userid | platform | track | trackid | bike | time | faults | date | verified | wr |
+----------+--------+----------+----------------+---------+-----------+---------+--------+---------------------+----------+-----+
| 412 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 90.456 | 6 | 2017-02-18 19:54:27 | yes | yes |
| 413 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 75.458 | 4 | 2017-02-18 19:54:39 | yes | yes |
| 414 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 77.885 | 2 | 2017-02-18 19:55:02 | yes | yes |
| 415 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 59.441 | 1 | 2017-02-18 19:55:12 | yes | yes |
| 416 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 52.145 | 0 | 2017-02-18 19:55:21 | yes | yes |
| 417 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 48.444 | 0 | 2017-02-18 19:55:26 | yes | yes |
| 418 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 42.753 | 0 | 2017-02-18 19:55:33 | yes | yes |
| 419 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 39.701 | 0 | 2017-02-18 19:55:42 | yes | yes |
| 420 | 1 | Xbox | Inferno IV | 40 | Pit Viper | 745.159 | 254 | 2017-02-18 20:17:35 | yes | yes |
| 421 | 1 | Xbox | Inferno IV | 40 | Pit Viper | 575.169 | 128 | 2017-02-18 20:17:50 | yes | yes |
| 422 | 1 | Xbox | Inferno IV | 40 | Pit Viper | 465.456 | 101 | 2017-02-18 20:18:12 | yes | yes |
| 423 | 1 | Xbox | Inferno IV | 40 | Pit Viper | 321.247 | 75 | 2017-02-18 20:18:29 | yes | yes |
| 424 | 1 | Xbox | Inferno IV | 40 | Pit Viper | 236.456 | 35 | 2017-02-18 20:18:58 | yes | yes |
| 425 | 1 | Xbox | Inferno IV | 40 | Pit Viper | 165.359 | 25 | 2017-02-18 20:19:17 | yes | yes |
| 426 | 1 | Xbox | Waterworks | 2 | Pit Viper | 45.452 | 457 | 2017-02-18 23:13:12 | yes | yes |
| 427 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 34.123 | 0 | 2017-02-18 23:21:47 | yes | yes |
| 428 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 32.254 | 0 | 2017-02-18 23:24:32 | yes | yes |
| 429 | 1 | Xbox | Turbine Terror | 1 | Pit Viper | 31.169 | 0 | 2017-02-18 23:25:33 | yes | yes |
| 430 | 1 | Xbox | Waterworks | 2 | Pit Viper | 50.000 | 0 | 2017-02-20 20:06:23 | yes | yes |
| 431 | 3 | Xbox | Turbine Terror | 1 | Pit Viper | 25.123 | 0 | 2017-02-20 20:21:54 | no | yes |
+----------+--------+----------+----------------+---------+-----------+---------+--------+---------------------+----------+-----+
我在开发中有一个查询:
SELECT users.username,
records.platform,
records.track,
records.bike,
records.time,
records.faults,
records.date
FROM records
INNER JOIN users
ON records.userid = users.userid
INNER JOIN (SELECT track, time, MIN(faults) AS faults
FROM records GROUP BY track) AS tracksWithMinFaults
ON records.track = tracksWithMinFaults.track
AND records.faults = tracksWithMinFaults.faults
WHERE users.userid = 1
ORDER BY records.trackid ASC
上述数据集查询的结果:
+----------------+----------+----------------+-----------+---------+--------+---------------------+
| username | platform | track | bike | time | faults | date |
+----------------+----------+----------------+-----------+---------+--------+---------------------+
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 52.145 | 0 | 2017-02-18 19:55:21 |
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 48.444 | 0 | 2017-02-18 19:55:26 |
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 42.753 | 0 | 2017-02-18 19:55:33 |
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 39.701 | 0 | 2017-02-18 19:55:42 |
| TheRealTeeHill | Xbox | Inferno IV | Pit Viper | 165.359 | 25 | 2017-02-18 20:19:17 |
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 34.123 | 0 | 2017-02-18 23:21:47 |
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 32.254 | 0 | 2017-02-18 23:24:32 |
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 31.169 | 0 | 2017-02-18 23:25:33 |
| TheRealTeeHill | Xbox | Waterworks | Pit Viper | 50.000 | 0 | 2017-02-20 20:06:23 |
+----------------+----------+----------------+-----------+---------+--------+---------------------+
一点背景:我试图为给定的用户ID提取每首曲目的“最佳”记录。最佳记录可以定义为具有最低故障和最短时间的记录。到目前为止我开发的查询是为具有最低故障的用户提取所有记录。查询还没有考虑时间!
我的问题:如何找到故障率最低的每条赛道的最短时间?
这是我在StackOverflow上的第一篇文章,如果我遗漏了任何内容或做错了什么,请告诉我。提前感谢您的时间:)
编辑:我在建议的重复帖子中找不到解决方案,因为我正在寻找2个最小值,首先我需要提取我的查询正在执行的所有最低故障记录,然后拉来自该组的最低时间记录?如果解决方案在其他帖子中,我看不到它:(
这是我想要实现的结果:
+----------------+----------+----------------+-----------+---------+--------+---------------------+
| username | platform | track | bike | time | faults | date |
+----------------+----------+----------------+-----------+---------+--------+---------------------+
| TheRealTeeHill | Xbox | Inferno IV | Pit Viper | 165.359 | 25 | 2017-02-18 20:19:17 |
| TheRealTeeHill | Xbox | Turbine Terror | Pit Viper | 31.169 | 0 | 2017-02-18 23:25:33 |
| TheRealTeeHill | Xbox | Waterworks | Pit Viper | 50.000 | 0 | 2017-02-20 20:06:23 |
+----------------+----------+----------------+-----------+---------+--------+---------------------+
答案 0 :(得分:2)
使用变量为每首曲目创建row_number
,但在这里我看到你可能会遇到关系问题。不知道你想怎么处理这些。
<强> DEMO 强>
SELECT *
FROM (
SELECT records.userid,
records.platform,
records.track,
records.bike,
records.time,
records.faults,
records.date,
@row := IF( @track = records.track,
@row + 1,
IF(@track := records.track, 1, 1)
) as rn
FROM records
CROSS JOIN (SELECT @row := 0, @track := '') as par
WHERE records.userid = 1
ORDER BY records.trackid ASC,
records.faults ASC,
records.time ASC
) as T
WHERE T.rn = 1;
<强>输出强>
答案 1 :(得分:0)
试试这个
SELECT
users.name,records.platform,records.track,records.min_time,records.bike,records.time,records.min_faults,records.date
FROM users
INNER JOIN
(SELECT MIN(time) as min_time,time, MIN(faults) AS min_faults,faults,platform,track,bike,date FROM records GROUP BY trackid) records
ON records.time = records.min_time
AND records.faults = records.min_faults
AND records.recordid = users.userid
WHERE users.userid = 1
ORDER BY records.trackid ASC;