无法在 MySQL 中过滤出具有相同标题的字段,无法使用 DISTINCT
SELECT america,
america_un,
game,
genre,
id,
platform,
url
FROM `main-games`
ORDER BY tally_rank
LIMIT 3
基本上我的问题是,我有时可能有两个游戏(不同的数据,只是相同的名称),它们的tally_rank-s彼此独立,给我重复的结果。本质上,我想做的就是使游戏栏与众不同,这样它就会忽略除我遇到的第一个游戏之外的任何其他同名游戏。
因此,如果我有“game1,game2,game2,game3”,我会想要第一个游戏2,而忽略第二个游戏。
不知道如何做到这一点,因为据我所知,DISTINCT 在 MySQL 中是全有或全无。
编辑:我应该注意到我对此使用了 GROUP BY 游戏,但由于某种原因它会跳过所有其他记录(2、4 等)。
编辑2:这是数据库中tally_rank的正确顺序:
Knight's Contract
Fight Night Champion
The Legend of Heroes: Trails in the Sky
PopCap Hits!
Killzone 3
将会发生的情况是查询将抓取1/3/5,如果我删除“GROUP BY game”,它会抓取1/2/3。不太确定为什么“GROUP BY game”会跳过记录。
SELECT america,
america_un,
game,
genre,
id,
platform,
url
FROM `main-games`
ORDER BY tally_rank
LIMIT 3
Basically my issue is that I may at times have two games (different data, just the same name) which have tally_rank-s which are one off from each other, giving me duplicates. Essentially what I'm wanting to do is make the game column distinct so it disregards any other games with the same name except for the first one I come across.
So if I had "game1, game2, game2, game3", I would want the first game2 and would disregard the second one.
Not sure how to do this since from what I can recall, DISTINCT is all-or-nothing in MySQL.
EDIT: I should note I've used GROUP BY game on this, but it skips every other record (2, 4, etc.) for some reason.
EDIT 2: Here is the correct order by tally_rank in the database:
Knight's Contract
Fight Night Champion
The Legend of Heroes: Trails in the Sky
PopCap Hits!
Killzone 3
What will happen is that the query will grab 1/3/5, and if I remove the "GROUP BY game", it grabs 1/2/3. Not really sure why "GROUP BY game" is skipping records.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当尝试
GROUP BY
一个字段,但在分组“之前”应用一些排序时(意思是 - 按游戏分组,但从每组相同的游戏中获取第一个游戏),我认为你可以使用内部选择:内部表有游戏名称和每个游戏的最小ID - 它告诉哪个是每个组中的第一个游戏。外部选择使用此 ID 来查找此特定游戏记录并为其获取其余数据。
When trying to
GROUP BY
a field, but applying some sorting "before" the grouping (meaning - group by game, but get me the first game from each group of identical games), I think you can use an inner select:The inner table has the game name and the minimum ID for each game - it tells which one is the first game in each group. The outer select uses this ID to find this specific game record and fetches the rest of the data for it.
没有区别:
有区别:
WITHOUT DISTINCT:
WITH DISTINCT:
如果你不能使用 Distinct 或 Group BY,也许可以尝试这个:
if you cant use Distinct or Group BY, maybe try this: