1

我在 PostgreSQL 中有这个查询:

select p1.id, p2.id, p1.title, p2.title 
from publication "p1", publication "p2"
where p1.title = p2.title and p1.id <> p2.id

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

id    id   title          title  
3456  5678 Ulysses        Ulysses  
5678  3456 Ulysses        Ulysses  
234,  345  Das Kapital    Das Kapital  
345   234  Das Kapital    Das Kapital 

我只需要第 1 行和第 3 行,或第 2 行和第 4 行。

4

3 回答 3

3
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 语法:

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
  ;
于 2013-03-20T18:55:39.043 回答
0

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

select p1.id, p2.id, p1.title, p2.title , sum(p1.id + p2.id) as temp
from publication "p1", publication "p2" group by temp
于 2013-03-28T05:09:46.507 回答
0
select DISTINCT p1.id, p2.id, p1.title, p2.title 
from publication "p1", publication "p2"
where p1.title = p2.title
于 2018-05-31T11:30:34.390 回答