SQL组使用distinct列

时间:2015-03-20 20:19:46

标签: mysql sql

我目前正在sql数据库中收集单圈时间,并且在使用最快的laptimes提取驱动程序时遇到一些困难!

结构如下所示!

 CREATE TABLE IF NOT EXISTS `leaderboard` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `driver` varchar(50) NOT NULL,
 `car` varchar(50) NOT NULL,
 `best` double NOT NULL,
 `guid` bigint(255) NOT NULL,
 `server_name` varchar(255) NOT NULL,
 `track` varchar(55) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `driver` (`driver`),
  KEY `server_name` (`server_name`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1213 ;

数据示例

INSERT INTO `leaderboard` (`id`, `driver`, `car`, `best`, `guid`, `server_name`, `track`) VALUES
(1, 'dave.38', 'bmw_m3_e30', 88.379, 76561198084629688, 'A++%21+A++%21+------+Saturdaynightracing.tk+-+%5BRACE-SERVER%5D+-+%5BMagione%5D+%23SNR', 'magione'),
(2, 'Gabriel Porfírio', 'bmw_m3_e30', 87.318, 76561197987062834, 'A++%21+A++%21+------+Saturdaynightracing.tk+-+%5BRACE-SERVER%5D+-+%5BMagione%5D+%23SNR', 'magione'),
(3, 'xX_VEGA_Xx', 'bmw_m3_e30', 88.23, 76561198182074333, 'A++%21+A++%21+------+Saturdaynightracing.tk+-+%5BRACE-SERVER%5D+-+%5BMagione%5D+%23SNR', 'magione'),
(4, 'dave.38', 'bmw_m3_e30', 88.379, 76561198084629688, 'A++%21+A++%21+------+Saturdaynightracing.tk+-+%5BRACE-SERVER%5D+-+%5BMagione%5D+%23SNR', 'magione'),
(5, 'Gabriel Porfírio', 'bmw_m3_e30', 87.318, 76561197987062834, 'A++%21+A++%21+------+Saturdaynightracing.tk+-+%5BRACE-SERVER%5D+-+%5BMagione%5D+%23SNR', 'magione');

现在我正在尝试使用以下SQL使用列best以最佳时间排序驱动程序,但看起来好像有时会被丢弃,排序和顺序的组合不起作用。

SELECT DISTINCT guid, car, best, driver FROM `leaderboard` WHERE `server_name` like '%%' AND `track` = 'magione' GROUP BY(driver) ORDER BY `best` * 1 LIMIT 10

请帮助这让我发疯!

1 个答案:

答案 0 :(得分:0)

您的数据中的某些字段不是很清楚,所以我做了这样的假设:

  • guid表示驱动程序的guid(因为数据中的同一驱动程序相同)。
  • car对于同一个驱动程序是相同的。

通过这些假设,您可以使用简单的GROUP BY来获得所需的结果:

SELECT driver, car, MIN(best) as best_time, guid
FROM leaderboard
WHERE `server_name` like '%%' AND `track` = 'magione'
GROUP BY driver, car, guid
ORDER BY MIN(best)