首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Postgresql:选择distinct -列组合

Postgresql:选择distinct -列组合
EN

Stack Overflow用户
提问于 2013-03-21 02:50:52
回答 3查看 2.6K关注 0票数 1

我在PostgreSQL中有这样的查询:

代码语言:javascript
复制
select p1.id, p2.id, p1.title, p2.title 
from publication "p1", publication "p2"
where p1.title = p2.title and p1.id <> p2.id

问题是它返回的数据比我需要的多:

代码语言:javascript
复制
id    id   title          title  
3456  5678 Ulysses        Ulysses  
5678  3456 Ulysses        Ulysses  
234,  345  Das Kapital    Das Kapital  
345   234  Das Kapital    Das Kapital 

我只需要第一行和第三行,或者第二行和第四行。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-21 02:55:39

代码语言:javascript
复制
select p1.id, p2.id
 , p1.title, p2.title
from publication p1
    , publication p2
where p1.title = p2.title
  and p1.id < p2.id -- tie breaker
  ;

或者使用更时髦的JOIN语法:

代码语言:javascript
复制
SELECT p1.id, p2.id
 , p1.title, p2.title
FROM publication p1
JOIN publication p2 ON p1.title = p2.title
                   AND p1.id < p2.id -- tie breaker
  ;
票数 3
EN

Stack Overflow用户

发布于 2013-03-28 13:09:47

我有一个简单的想法来实现你的场景。

代码语言:javascript
复制
select p1.id, p2.id, p1.title, p2.title , sum(p1.id + p2.id) as temp
from publication "p1", publication "p2" group by temp
票数 0
EN

Stack Overflow用户

发布于 2018-05-31 19:30:34

代码语言:javascript
复制
select DISTINCT p1.id, p2.id, p1.title, p2.title 
from publication "p1", publication "p2"
where p1.title = p2.title
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15532237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档