嗨我一直在寻找这个,我找到了其他示例代码的解决方案,但我不知道如何为我的实现。
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 分组的原因。目标是以最低的价格获得独特的游戏DBGames。
我有的。
+--------------+---------+----+---------+
| 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(马克) 马克提出的解决方案给了我这个错误。
- 还有gameDBGames重复。
- 有些游戏没有显示。
我更新了 WHERE 子句以防万一。