【发布时间】:2023-03-14 04:18:02
【问题描述】:
这是我的查询:
SELECT autor.entwickler,anwendung.name
FROM autor
left join anwendung
on anwendung.name = autor.anwendung;
entwickler | name
------------+-------------
Benutzer 1 | Anwendung 1
Benutzer 2 | Anwendung 1
Benutzer 2 | Anwendung 2
Benutzer 1 | Anwendung 3
Benutzer 1 | Anwendung 4
Benutzer 2 | Anwendung 4
(6 rows)
我想为字段 name 中的每个不同值保留一行,并像这样丢弃其他值:
entwickler | name
------------+-------------
Benutzer 1 | Anwendung 1
Benutzer 2 | Anwendung 2
Benutzer 1 | Anwendung 3
Benutzer 1 | Anwendung 4
在 MySQL 中我会这样做:
SELECT autor.entwickler,anwendung.name
FROM autor
left join anwendung
on anwendung.name = autor.anwendung
GROUP BY anwendung.name;
但是 PostgreSQL 给了我这个错误:
错误:列“auto.entwickler”必须出现在 GROUP BY 子句中 或用于聚合函数第 1 行:SELECT autor.entwickler FROM autor left join anwendung on an ...
我完全理解错误并假设 mysql 实现比 postgres 实现更不符合 SQL。但是我怎样才能得到想要的结果呢?
【问题讨论】:
-
您的 MySQL 示例在非标准 SQL 模式下工作,而 PostgreSQL 使用标准 SQL...要比较,您必须在 MySQL 中使用
ONLY_FULL_GROUP_BY模式。即使在 MySQL 中,您也需要一个聚合采样器函数(ANY_VALUE由 Craig Ringer 评论)...另见 dba.stackexchange.com/a/133747/90651
标签: mysql sql postgresql select duplicates