我在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行。
答案 0 :(得分: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
;
答案 1 :(得分: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
答案 2 :(得分:0)
select DISTINCT p1.id, p2.id, p1.title, p2.title
from publication "p1", publication "p2"
where p1.title = p2.title