无法在 MySQL 中过滤出具有相同标题的字段,无法使用 DISTINCT

发布于 2024-10-19 23:03:05 字数 733 浏览 4 评论 0原文

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左岸枫 2024-10-26 23:03:05

当尝试GROUP BY一个字段,但在分组“之前”应用一些排序时(意思是 - 按游戏分组,但从每组相同的游戏中获取第一个游戏),我认为你可以使用内部选择:

SELECT m.america,
       m.america_un,
       m.game,
       m.genre,
       m.id,
       m.platform,
       m.url
FROM `main-games` m
JOIN (SELECT game,
       min(id) AS id
       FROM `main-games`
       ORDER BY tally_rank
       GROUP BY game) as t
ON m.game = t.game AND m.id = t.id
ORDER BY m.tally_rank
LIMIT 3

内部表有游戏名称和每个游戏的最小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:

SELECT m.america,
       m.america_un,
       m.game,
       m.genre,
       m.id,
       m.platform,
       m.url
FROM `main-games` m
JOIN (SELECT game,
       min(id) AS id
       FROM `main-games`
       ORDER BY tally_rank
       GROUP BY game) as t
ON m.game = t.game AND m.id = t.id
ORDER BY m.tally_rank
LIMIT 3

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.

哆兒滾 2024-10-26 23:03:05

没有区别:

 SELECT field1,field2,field3,game FROM `main-games` GROUP BY game LIMIT 3;

有区别:

 SELECT DISTINCT(game),field1,field2 FROM `main-games` ORDER BY tally_rank LIMIT 3;

WITHOUT DISTINCT:

 SELECT field1,field2,field3,game FROM `main-games` GROUP BY game LIMIT 3;

WITH DISTINCT:

 SELECT DISTINCT(game),field1,field2 FROM `main-games` ORDER BY tally_rank LIMIT 3;
我们的影子 2024-10-26 23:03:05

如果你不能使用 Distinct 或 Group BY,也许可以尝试这个:

SELECT america,         
america_un,         
game,         
genre,         
id,         
platform,         
url  
FROM   `main-games` M1 LEFT JOIN `main-games` M2 
ON (M1.game = M2.game AND M1.id > M2.id)
WHERE M2.id IS NULL
ORDER  BY tally_rank  LIMIT  3 

if you cant use Distinct or Group BY, maybe try this:

SELECT america,         
america_un,         
game,         
genre,         
id,         
platform,         
url  
FROM   `main-games` M1 LEFT JOIN `main-games` M2 
ON (M1.game = M2.game AND M1.id > M2.id)
WHERE M2.id IS NULL
ORDER  BY tally_rank  LIMIT  3 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文