Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 对多个列使用select distinct,但所有列都不能是不同的_Sql_Oracle - Fatal编程技术网

Sql 对多个列使用select distinct,但所有列都不能是不同的

Sql 对多个列使用select distinct,但所有列都不能是不同的,sql,oracle,Sql,Oracle,我需要一个表中的所有列,但两列必须是不同的。 我使用了这段代码,但它检查了所有列,但我只需要其中两列是不同的。 我怎样才能满足这个要求 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 如果其他列不同,会发生什么情况?数据库应该

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

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

如果其他列不同,会发生什么情况?数据库应该如何选择要显示的值?答案是“它不能”,因为它不能,所以没有语法可以这样做(这是一个逻辑错误)

MySQL确实有一个扩展,它将随机选取值

select a.personalId, a.fileId, a.name, a.surname, a.address
from my_table a
group by a.personalId, a.fileId

但我不建议依赖这个。您需要重新考虑正在使用的逻辑。

Postgresql支持DISTINCT ON语法:


有关如何在Oracle中执行此操作,请参阅。

您可以发布示例数据和所需结果吗?实际上,您使用的是Oracle SQL--PL/SQL是Oracle的过程语言。您说得对,我是数据库编程新手。谢谢,此链接很有用;)
select distinct on (a.personalId, a.fileId) a.personalId, a.fileId, a.name, a.surname, a.address
from my_table a