gpt4 book ai didi

sql - 对多列使用 select distinct,但所有列都必须不同

转载 作者:行者123 更新时间:2023-12-02 08:44:57 31 4
gpt4 key购买 nike

我需要一个表中的所有列,但两列必须不同。我使用了这段代码,但它检查了所有列,但我只需要其中两个是不同的。我怎样才能满足这个要求?

select distinct a.personalId, a.fileId, a.name, a.surname, a.address from my_table a

如果我使用下面的代码,我无法获取其他列:

select distinct a.personalId, a.fileId from my_table a

最佳答案

Postgresql 支持 DISTINCT ON 语法:http://www.postgresql.org/docs/current/interactive/sql-select.html

select distinct on (a.personalId, a.fileId) a.personalId, a.fileId, a.name, a.surname, a.address
from my_table a

参见 Oracle equivalent of Postgres' DISTINCT ON?了解如何在 Oracle 中执行此操作。

关于sql - 对多列使用 select distinct,但所有列都必须不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13174168/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com