我想列出仅有独特持有人的前6名比赛记录。我的意思是列表中的持有人不应该与他的另一个记录一起列出。我目前使用下面的查询列出前6次。
mysql> select * from racerecords order by record_time asc, date asc;
+----+---------+------------+-------------+---------------------+----------+
| id | race_id | holder | record_time | date | position |
+----+---------+------------+-------------+---------------------+----------+
| 2 | 10 | Stav | 15 | 2014-08-11 19:43:49 | 1 |
| 1 | 10 | Jennifer | 15 | 2014-08-13 19:43:19 | 1 |
| 4 | 10 | Jennifer | 16 | 2014-08-02 19:44:27 | 1 |
| 5 | 10 | Osman | 17 | 2014-08-04 19:44:57 | 1 |
| 7 | 10 | Gokhan | 18 | 2014-08-15 19:45:37 | 1 |
| 3 | 10 | MotherLode | 25 | 2014-08-01 19:44:11 | 1 |
+----+---------+------------+-------------+---------------------+----------+
6 rows in set (0.00 sec)
正如你可以看到持有人" Jennifer"列出两次。我希望mySQL在进入列表后跳过她。我想要生成的结果是:
+----+---------+------------+-------------+---------------------+----------+
| id | race_id | holder | record_time | date | position |
+----+---------+------------+-------------+---------------------+----------+
| 2 | 10 | Stav | 15 | 2014-08-11 19:43:49 | 1 |
| 1 | 10 | Jennifer | 15 | 2014-08-13 19:43:19 | 1 |
| 5 | 10 | Osman | 17 | 2014-08-04 19:44:57 | 1 |
| 7 | 10 | Gokhan | 18 | 2014-08-15 19:45:37 | 1 |
| 3 | 10 | MotherLode | 25 | 2014-08-01 19:44:11 | 1 |
+----+---------+------------+-------------+---------------------+----------+
我尝试了一切。 GROUP BY holder
会产生错误的结果。它获得了持有者的第一个记录,即使不是最好的。在此表中,它会生成如上所示的输出,因为 id:1 是我为Jennifer插入的第一条记录。
如何生成输出结果如上?
答案 0 :(得分:0)
通过此查询可以实现所需的结果,但性能密集。我已在SQLFilddle http://sqlfiddle.com/#!2/f8ee7/3
中复制了结果select * from racerecords
where
(HOLDER, RECORD_TIME) in (
select HOLDER,min(RECORD_TIME) from racerecords
group by HOLDER)
似乎你错过了在子查询中包含Where子句。试试这个
select * from racerecords
where
(HOLDER, RECORD_TIME) in (
select HOLDER,min(RECORD_TIME) from racerecords where race_id =17
group by HOLDER )
And race_id =17
Order by RECORD_TIME
答案 1 :(得分:0)
你应该使用distinct子句
SELECT DISTINCT column_name,column_name
FROM table_name;