简体   繁体   English

根据2个条件选择行

[英]Selecting rows based on 2 conditions

I have 2 tables, records: 我有2张桌子,记录:

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`)
)

and users: 和用户:

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`)
) 

I have some example data stored: 我存储了一些示例数据:

 +----------+--------+----------+----------------+---------+-----------+---------+--------+---------------------+----------+-----+
| 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 |
+----------+--------+----------+----------------+---------+-----------+---------+--------+---------------------+----------+-----+

I have a query in development: 我在开发中有一个查询:

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

The results from the query on the data set above: 对以上数据集的查询结果:

+----------------+----------+----------------+-----------+---------+--------+---------------------+
|    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 |
+----------------+----------+----------------+-----------+---------+--------+---------------------+

A little background: I am trying to pull the "best" record for each track for a given user ID. 一些背景知识:我正在尝试为给定用户ID的每条曲目提取“最佳”记录。 The best record can be defined as the record with the lowest faults and lowest time. 最佳记录可以定义为故障最少,时间最少的记录。 The query I have developed so far is pulling all the records for a user that have the lowest faults. 到目前为止,我开发的查询正在拉取故障最少的用户的所有记录。 The query does not consider time yet! 查询尚未考虑时间!

My question: How do I find the lowest time for each track with the lowest faults? 我的问题:如何找到故障最少的每个轨道的最短时间?

This is my first post on StackOverflow, if I've missed anything or done something wrong please let me know. 这是我在StackOverflow上的第一篇文章,如果我错过了任何事情或做错了什么,请告诉我。 Thanks in advance for your time :) 在此先感谢您的时间 :)

Edit: I can't see a solution in the suggested duplicate post as I am looking for 2 minimum values, first I need to pull all of the lowest fault records, which my query is doing, and then pull the lowest time records from that set? 编辑:我正在寻找2个最小值时,在建议的重复帖子中看不到解决方案,首先我需要提取查询所执行的所有最低故障记录,然后从中提取最低时间记录组? If the solution is in that other post I cannot see it :( 如果解决方案在其他帖子中,我看不到:(

This is the result I'm trying to achieve: 这是我想要达到的结果:

+----------------+----------+----------------+-----------+---------+--------+---------------------+
|    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 |
+----------------+----------+----------------+-----------+---------+--------+---------------------+

Use variables to create a row_number for each track, but here I see you may have an issue with ties. 使用变量为每个音轨创建一个row_number ,但是在这里我看到您可能对纽带有疑问。 Not sure how you want handle those. 不确定如何处理这些问题。

DEMO 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;     

OUTPUT OUTPUT

在此处输入图片说明

try this 尝试这个

   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;

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

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