接收在去重查询中的完整行。

huangapple go评论68阅读模式
英文:

Receive full row in distinct query

问题

我有一个包含3个字段的mdb数据库中的表:

ID | API密钥 | 姓名

这个表可以包含多个数据集,其中API密钥可以相同。

现在我只需要获取具有不同API密钥的数据集。

例如,对于以下数据集:

1 | abc | 测试1
2 | def | 测试2
3 | abc | 测试3
4 | xyz | 测试4
5 | xyz | 测试5
6 | abc | 测试6

我需要得到类似以下的结果:

1 | abc | 测试1
2 | def | 测试2
4 | xyz | 测试4

具有相同API密钥的数据集中返回哪一个并不重要。

我已经在谷歌上搜索了大约30分钟,但没有找到符合我的需求的内容。也尝试了一些方法,但没有找到有效的解决方案。

我希望有人有想法。

英文:

I have a table with let's say 3 Fields in a mdb-database:

ID | API-Key | Name

This table can contain multiple datasets where the API-Key can be identical.

Now I need to get only datasets with distinct API-Keys.

So For example with the following datasets:

1 | abc | Test1
2 | def | Test2
3 | abc | Test3
4 | xyz | Test4
5 | xyz | Test5
6 | abc | Test6

I need to get something like:

1 | abc | Test1
2 | def | Test2
4 | xyz | Test4

It doesn't matter which of the datasets with identical API-Key is returned.

I already searched google for about 30 min. but doesn't found anything matching my needs.
Also tried around a bit but didn't found any working solution.

I hope someone has an idea.

答案1

得分: 2

即使您不在乎,数据库引擎也需要知道要选择哪些记录/值。因此,您需要一些聚合函数,例如MIN()

DISTINCT 只会在您只想检索API密钥时有所帮助,如果您想要完整的记录,您需要分组。

SELECT Min(ID) AS SomeID, API_Key, Min(Name) AS SomeName
FROM YourTable
GROUP BY API_Key
英文:

Even if you don't care, the database engine needs to know which records/values to select. So you need some aggregate function, e.g. MIN()

DISTINCT would only help if you just want to retrieve the API keys, if you want full records you need grouping.

SELECT Min(ID) AS SomeID, API_Key, Min(Name) AS SomeName
FROM YourTable
GROUP BY API_Key

huangapple
  • 本文由 发表于 2023年7月10日 19:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定