您好我一直在寻找这个,我找到了其他示例代码的解决方案,但我无法弄清楚如何为我的实现。
SELECT `gameDBGames`, `game_id`, MIN(`gamePrice`) AS `gamePrice`
FROM `games`
LEFT JOIN `platforms` ON `gamePlatform` = `platform_id`
LEFT JOIN `bundles` ON `gameBundle` = `bundle_id`
LEFT JOIN `currency` ON `bundleCurrency` = `currency_id`
WHERE `bundleEnd` > CURDATE() AND `bundleType` = "1" AND `gameDBGames` != "0"
GROUP BY `gameDBGames`
这是我的实际查询。这会返回最低价格,但不会与game_id
对应。我怎么能这样做?我相信正在做这样的内部联接:
SELECT `gameDBGames`, `game_id`, MIN(`gamePrice`) AS `gamePrice`
FROM `games`
LEFT JOIN `platforms` ON `gamePlatform` = `platform_id`
LEFT JOIN `bundles` ON `gameBundle` = `bundle_id`
INNER JOIN (....)
LEFT JOIN `currency` ON `bundleCurrency` = `currency_id`
WHERE b.`bundleEnd` > CURRDATE() AND b.`bundleType` = "1" AND g.`gameDBGames` != "0"
谢谢。
编辑:抱歉,我不知道我在考虑不发布表格结构。
game_id是唯一ID(例如同一游戏,但价格不同),gameDBGames是游戏的ID(例如gameDBGames = 1,价格可能是40或30),这就是我使用gameDBGames进行分组的原因。目标是以最低价格获得独特的gameDBGames。
我有什么。
+--------------+---------+----+---------+
| table games | | | |
+--------------+---------+----+---------+
| game_id | int | AI | PRIMARY |
| gameDBGames | int | | |
| gamePrice | float | | |
| gamePlatform | tinyint | | |
| gameBundle | int | | |
+--------------+---------+----+---------+
+---------+-------------+-----------+--------------+------------+
| game_id | gameDBGames | gamePrice | gamePlatform | gameBundle |
+---------+-------------+-----------+--------------+------------+
| 1 | 1 | 20 | 1 | 1 |
| 2 | 2 | 20 | 2 | 1 |
| 3 | 2 | 15 | 2 | 1 |
| 4 | 3 | 17 | 1 | 1 |
| 5 | 3 | 20 | 1 | 1 |
| 6 | 3 | 15 | 1 | 1 |
| 7 | 4 | 16 | 2 | 2 |
| 8 | 5 | 18 | 2 | 2 |
| 9 | 5 | 14 | 2 | 2 |
| 10 | 6 | 15 | 1 | 2 |
+---------+-------------+-----------+--------------+------------+
我得到了什么。
+---------+-------------+-----------+--------------+------------+
| game_id | gameDBGames | gamePrice | gamePlatform | gameBundle |
+---------+-------------+-----------+--------------+------------+
| 1 | 1 | 20 | 1 | 1 |
| 2 | 2 | 15 | 2 | 1 |
| 4 | 3 | 15 | 1 | 1 |
| 7 | 4 | 16 | 2 | 2 |
| 8 | 5 | 14 | 2 | 2 |
| 10 | 6 | 15 | 1 | 2 |
+---------+-------------+-----------+--------------+------------+
正如您所看到的,game_id与gamePrice不对应。它应该是这样的。
+---------+-------------+-----------+--------------+------------+
| game_id | gameDBGames | gamePrice | gamePlatform | gameBundle |
+---------+-------------+-----------+--------------+------------+
| 1 | 1 | 20 | 1 | 1 |
| 3 | 2 | 15 | 2 | 1 |
| 6 | 3 | 15 | 1 | 1 |
| 7 | 4 | 16 | 2 | 2 |
| 9 | 5 | 14 | 2 | 2 |
| 10 | 6 | 15 | 1 | 2 |
+---------+-------------+-----------+--------------+------------+
希望你现在明白,对不起之前没有得到更多的解释。如果你需要别的东西请问。谢谢。
编辑2(标记) Mark提出的解决方案给了我这个错误。
我已经更新了WHERE子句以防万一。
答案 0 :(得分:1)
尝试:
SELECT g.`gameDBGames`, g.`game_id`, g.`gamePrice`
FROM (SELECT `gameDBGames`, MIN(`gamePrice`) AS `minPrice`
FROM `games`
GROUP BY `gameDBGames`) mn
JOIN `games` g
ON mn.`gameDBGames`=g.`gameDBGames` and mn.`minPrice`=g.`gamePrice`
LEFT JOIN `platforms` p ON g.`gamePlatform` = p.`platform_id`
LEFT JOIN `bundles` b ON g.`gameBundle` = b.`bundle_id`
LEFT JOIN `currency` c ON b.`bundleCurrency` = c.`currency_id`
WHERE ....
由于子查询中的分组,不应要求主查询中的GROUP BY
- 但是,对于给定的game_id
,将返回多个gameDBGames
如果有多个game_id
具有相同gameDBGames
的最低价格。