-2

嗨我一直在寻找这个,我找到了其他示例代码的解决方案,但我不知道如何为我的实现。

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 子句以防万一。

4

1 回答 1

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_idgameDBGames

于 2013-06-02T20:31:52.833 回答