MYSQL GROUPBY返回不正确的值

时间:2018-11-08 19:43:28

标签: mysql

我遇到了与在查询中使用GROUPBY有关的问题。当我同时按p.ID和cc.CarClassHash分组时,将返回重复的行。但是,当我仅按p.ID分组时,仅按一个p.ID和cc.CarClassHash进行查询即可。但是,问题是与p.ID返回的cc.carClassHash值是随机的。底线是如果我将两个值分组,我将获得所有带有多个p.ID返回的数据。如果我按一组分组,我会得到一个p.ID,但仍然是错车。

有没有办法确保cc.carClassHash值对应于正确的最小e.alternateEventDurationInMilliseconds值?

我在示例数据中随附了该问题的屏幕截图。 https://imgur.com/a/8N4spFK 前两行仅按p.ID分组(按预期一个p.ID,但随机分配汽车价值)。底部的四行按p.ID和cc.ownedCarId分组(具有正确的汽车,但现在有多个数据p.ID。我希望得到的结果是,每个具有cc.ownedCarId的p.ID的最短时间不包括同一位玩家的其他任何结果。

这是我正在使用的代码,当按两个值分组时,它将返回重复数据。

SELECT p.ID, e.personaId, e.ID AS event_id, e.EVENTID, e.carId, 
         MIN(e.alternateEventDurationInMilliseconds) AS alternateEventDurationInMilliseconds,
           p.iconIndex,
           p.name AS p_name, cc.name AS c_name, cc.carClassHash AS c_hash, ev.carClassHash, ccs.full_name AS cs_name, b.user_id AS banned_status, ev.legitTime, e.bustedCount, e.distanceToFinish, e.finishReason, e.fractionCompleted
           FROM EVENT_DATA e
    INNER JOIN PERSONA p ON e.personaId = p.ID
    INNER JOIN CUSTOMCAR cc ON cc.ownedCarId = e.carId 
    INNER JOIN CAR_CLASSES ccs ON ccs.store_name = cc.name
    INNER JOIN USER u ON u.ID = p.USERID
    LEFT JOIN BAN b ON b.user_id = u.ID
    INNER JOIN EVENTSAVED ev ON ev.ID = e.EVENTID


    WHERE (e.EVENTID = '28' AND e.alternateEventDurationInMilliseconds > '0' AND e.bustedCount < '1' AND e.distanceToFinish = '0' AND e.fractionCompleted > '0.9' AND cc.carClassHash = ev.carClassHash AND e.alternateEventDurationInMilliseconds > ev.legitTime AND e.finishReason != '2' OR e.finishReason = '1')

GROUP BY p.ID, cc.ownedCarId

ORDER BY alternateEventDurationInMilliseconds ASC



      CREATE TABLE `EVENT` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `baseCashReward` int(11) NOT NULL,
 `baseRepReward` int(11) NOT NULL,
 `eventModeId` int(11) NOT NULL,
 `finalCashRewardMultiplier` float NOT NULL,
 `finalRepRewardMultiplier` float NOT NULL,
 `isEnabled` bit(1) NOT NULL,
 `isLocked` bit(1) NOT NULL,
 `legitTime` bigint(20) NOT NULL,
 `levelCashRewardMultiplier` float NOT NULL,
 `levelRepRewardMultiplier` float NOT NULL,
 `maxCarClassRating` int(11) NOT NULL,
 `maxLevel` int(11) NOT NULL,
 `maxPlayers` int(11) NOT NULL,
 `minCarClassRating` int(11) NOT NULL,
 `minLevel` int(11) NOT NULL,
 `minTopSpeedTrigger` float NOT NULL,
 `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `perfectStartCashMultiplier` float NOT NULL,
 `perfectStartRepMultiplier` float NOT NULL,
 `rank1CashMultiplier` float NOT NULL,
 `rank1RepMultiplier` float NOT NULL,
 `rank2CashMultiplier` float NOT NULL,
 `rank2RepMultiplier` float NOT NULL,
 `rank3CashMultiplier` float NOT NULL,
 `rank3RepMultiplier` float NOT NULL,
 `rank4CashMultiplier` float NOT NULL,
 `rank4RepMultiplier` float NOT NULL,
 `rank5CashMultiplier` float NOT NULL,
 `rank5RepMultiplier` float NOT NULL,
 `rank6CashMultiplier` float NOT NULL,
 `rank6RepMultiplier` float NOT NULL,
 `rank7CashMultiplier` float NOT NULL,
 `rank7RepMultiplier` float NOT NULL,
 `rank8CashMultiplier` float NOT NULL,
 `rank8RepMultiplier` float NOT NULL,
 `topSpeedCashMultiplier` float NOT NULL,
 `topSpeedRepMultiplier` float NOT NULL,
 `carClassHash` int(11) NOT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=536 DEFAULT CHARSET=utf8

CREATE TABLE `EVENT_DATA` (
  `ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `alternateEventDurationInMilliseconds` bigint(20) NOT NULL,
  `bestLapDurationInMilliseconds` bigint(20) NOT NULL,
  `bustedCount` int(11) NOT NULL,
  `carId` bigint(20) NOT NULL,
  `copsDeployed` int(11) NOT NULL,
  `copsDisabled` int(11) NOT NULL,
  `copsRammed` int(11) NOT NULL,
  `costToState` int(11) NOT NULL,
  `distanceToFinish` float NOT NULL,
  `eventDurationInMilliseconds` bigint(20) NOT NULL,
  `eventModeId` int(11) NOT NULL,
  `eventSessionId` bigint(20) DEFAULT NULL,
  `finishReason` int(11) NOT NULL,
  `fractionCompleted` float NOT NULL,
  `hacksDetected` bigint(20) NOT NULL,
  `heat` float NOT NULL,
  `infractions` int(11) NOT NULL,
  `longestJumpDurationInMilliseconds` bigint(20) NOT NULL,
  `numberOfCollisions` int(11) NOT NULL,
  `perfectStart` int(11) NOT NULL,
  `personaId` bigint(20) DEFAULT NULL,
  `rank` int(11) NOT NULL,
  `roadBlocksDodged` int(11) NOT NULL,
  `spikeStripsDodged` int(11) NOT NULL,
  `sumOfJumpsDurationInMilliseconds` bigint(20) NOT NULL,
  `topSpeed` float NOT NULL,
  `EVENTID` int(11) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `FK_EVENTDATA_EVENT` (`EVENTID`),
  CONSTRAINT `FK_EVENTDATA_EVENT` FOREIGN KEY (`EVENTID`) REFERENCES `EVENT` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=157134 DEFAULT CHARSET=utf8  

CREATE TABLE `PERSONA` (
 `ID` bigint(20) NOT NULL AUTO_INCREMENT,
 `boost` double NOT NULL,
 `cash` double NOT NULL,
 `created` datetime DEFAULT NULL,
 `curCarIndex` int(11) NOT NULL,
 `iconIndex` int(11) NOT NULL,
 `level` int(11) NOT NULL,
 `motto` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `percentToLevel` float NOT NULL,
 `rating` double NOT NULL,
 `rep` double NOT NULL,
 `repAtCurrentLevel` int(11) NOT NULL,
 `score` int(11) NOT NULL,
 `USERID` bigint(20) DEFAULT NULL,
 `badges` varchar(2048) DEFAULT NULL,
 PRIMARY KEY (`ID`),
 KEY `FK_PERSONA_USER` (`USERID`),
 CONSTRAINT `FK_PERSONA_USER` FOREIGN KEY (`USERID`) REFERENCES `USER` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4043 DEFAULT CHARSET=utf8

CREATE TABLE `CUSTOMCAR` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `baseCar` int(11) NOT NULL,
 `carClassHash` int(11) NOT NULL,
 `isPreset` bit(1) NOT NULL,
 `level` int(11) NOT NULL,
 `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `physicsProfileHash` int(11) NOT NULL,
 `rating` int(11) NOT NULL,
 `resalePrice` float NOT NULL,
 `rideHeightDrop` float NOT NULL,
 `skillModSlotCount` int(11) NOT NULL,
 `version` int(11) NOT NULL,
 `ownedCarId` bigint(20) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `FK_CUSTOMCAR_OWNEDCAR` (`ownedCarId`),
 CONSTRAINT `FK_CUSTOMCAR_OWNEDCAR` FOREIGN KEY (`ownedCarId`) REFERENCES `OWNEDCAR` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16087 DEFAULT CHARSET=utf8

CREATE TABLE `CAR_CLASSES` (
 `store_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `ac_stock` int(11) DEFAULT NULL,
 `ac_var1` int(11) DEFAULT NULL,
 `ac_var2` int(11) DEFAULT NULL,
 `ac_var3` int(11) DEFAULT NULL,
 `full_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `ha_stock` int(11) DEFAULT NULL,
 `ha_var1` int(11) DEFAULT NULL,
 `ha_var2` int(11) DEFAULT NULL,
 `ha_var3` int(11) DEFAULT NULL,
 `hash` int(11) DEFAULT NULL,
 `manufactor` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `model` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `product_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `ts_stock` int(11) DEFAULT NULL,
 `ts_var1` int(11) DEFAULT NULL,
 `ts_var2` int(11) DEFAULT NULL,
 `ts_var3` int(11) DEFAULT NULL,
 PRIMARY KEY (`store_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `USER` (
 `ID` bigint(20) NOT NULL AUTO_INCREMENT,
 `created` datetime DEFAULT NULL,
 `EMAIL` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `HWID` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `IP_ADDRESS` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `isAdmin` bit(1) DEFAULT NULL,
 `lastLogin` datetime DEFAULT NULL,
 `PASSWORD` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `premium` bit(1) DEFAULT NULL,
 `gameHardwareHash` varchar(255) DEFAULT NULL,
 `authservUUID` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3887 DEFAULT CHARSET=utf8

CREATE TABLE `BAN` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `data` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `endsAt` datetime DEFAULT NULL,
 `reason` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `user_id` bigint(20) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `FK_BAN_USER` (`user_id`),
 CONSTRAINT `FK_BAN_USER` FOREIGN KEY (`user_id`) REFERENCES `USER` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8

CREATE TABLE `EVENTSAVED` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `baseCashReward` int(11) NOT NULL,
 `baseRepReward` int(11) NOT NULL,
 `eventModeId` int(11) NOT NULL,
 `finalCashRewardMultiplier` float NOT NULL,
 `finalRepRewardMultiplier` float NOT NULL,
 `isEnabled` bit(1) NOT NULL,
 `isLocked` bit(1) NOT NULL,
 `legitTime` bigint(20) NOT NULL,
 `levelCashRewardMultiplier` float NOT NULL,
 `levelRepRewardMultiplier` float NOT NULL,
 `maxCarClassRating` int(11) NOT NULL,
 `maxLevel` int(11) NOT NULL,
 `maxPlayers` int(11) NOT NULL,
 `minCarClassRating` int(11) NOT NULL,
 `minLevel` int(11) NOT NULL,
 `minTopSpeedTrigger` float NOT NULL,
 `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `perfectStartCashMultiplier` float NOT NULL,
 `perfectStartRepMultiplier` float NOT NULL,
 `rank1CashMultiplier` float NOT NULL,
 `rank1RepMultiplier` float NOT NULL,
 `rank2CashMultiplier` float NOT NULL,
 `rank2RepMultiplier` float NOT NULL,
 `rank3CashMultiplier` float NOT NULL,
 `rank3RepMultiplier` float NOT NULL,
 `rank4CashMultiplier` float NOT NULL,
 `rank4RepMultiplier` float NOT NULL,
 `rank5CashMultiplier` float NOT NULL,
 `rank5RepMultiplier` float NOT NULL,
 `rank6CashMultiplier` float NOT NULL,
 `rank6RepMultiplier` float NOT NULL,
 `rank7CashMultiplier` float NOT NULL,
 `rank7RepMultiplier` float NOT NULL,
 `rank8CashMultiplier` float NOT NULL,
 `rank8RepMultiplier` float NOT NULL,
 `topSpeedCashMultiplier` float NOT NULL,
 `topSpeedRepMultiplier` float NOT NULL,
 `carClassHash` int(11) NOT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=536 DEFAULT CHARSET=utf8

CREATE TABLE `OWNEDCAR` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `durability` int(11) NOT NULL,
 `expirationDate` datetime DEFAULT NULL,
 `heat` float NOT NULL,
 `ownershipType` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
 `carSlotId` bigint(20) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `FK_OWNEDCAR_CARSLOT` (`carSlotId`),
 CONSTRAINT `FK_OWNEDCAR_CARSLOT` FOREIGN KEY (`carSlotId`) REFERENCES `CARSLOT` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16088 DEFAULT CHARSET=utf8

CREATE TABLE `CARSLOT` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `PersonaId` bigint(20) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `FK_CARSLOT_PERSONA` (`PersonaId`),
 CONSTRAINT `FK_CARSLOT_PERSONA` FOREIGN KEY (`PersonaId`) REFERENCES `PERSONA` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=16088 DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:1)

好的,这将为您提供所需的内容,但有一个警告:

SELECT p.ID, e.personaId, e.ID AS event_id, e.EVENTID, e.carId, e.alternateEventDurationInMilliseconds,
p.iconIndex, p.name AS p_name, cc.name AS c_name, cc.carClassHash AS c_hash, ev.carClassHash, ccs.full_name AS cs_name, b.user_id AS banned_status, 
ev.legitTime, e.bustedCount, e.distanceToFinish, e.finishReason, e.fractionCompleted

FROM EVENT_DATA e

INNER JOIN (SELECT personaId, MIN(alternateEventDurationInMilliseconds) AS alternateEventDurationInMilliseconds
        FROM EVENT_DATA
        GROUP BY personaId) f
    ON f.personaId = e.personaId AND f.alternateEventDurationInMilliseconds = e.alternateEventDurationInMilliseconds

INNER JOIN PERSONA p 
    ON p.ID = e.personaId 
INNER JOIN CUSTOMCAR cc 
    ON cc.ownedCarId = e.carId 
INNER JOIN EVENTSAVED ev 
    ON ev.ID = e.EVENTID
INNER JOIN CAR_CLASSES ccs 
    ON ccs.store_name = cc.name
INNER JOIN USER u 
    ON u.ID = p.USERID   
LEFT JOIN BAN b 
    ON b.user_id = u.ID

WHERE e.finishReason = '1'
OR (e.EVENTID = '28' 
    AND e.alternateEventDurationInMilliseconds > '0' 
    AND e.bustedCount < '1' 
    AND e.distanceToFinish = '0' 
    AND e.fractionCompleted > '0.9' 
    AND cc.carClassHash = ev.carClassHash 
    AND e.alternateEventDurationInMilliseconds > ev.legitTime 
    AND e.finishReason != '2')

ORDER BY e.alternateEventDurationInMilliseconds ASC

注意事项

一个警告是,如果同一位用户使用另一辆车在同一时间获得了EXACT。该用户实际上将拥有2种不同汽车的2次“最佳时光”。

让我知道这是否对您有用。