[英]how to compare and extract 2 columns PostgreSQL
I want to extract colums that different in fruit colum but have a same value in fruit_id colum.我想提取水果列中不同但在fruit_id 列中具有相同值的列。 how's the query?
查询如何? please help
请帮忙
fruit![]() |
fruit_id![]() |
---|---|
apple![]() |
a1 ![]() |
banana![]() |
b1 ![]() |
citrus![]() |
c1 ![]() |
manggo![]() |
a1 ![]() |
orange![]() |
c1 ![]() |
I expected the result is:我预计结果是:
fruit![]() |
fruit_id![]() |
---|---|
apple![]() |
a1 ![]() |
manggo![]() |
a1 ![]() |
citrus![]() |
c1 ![]() |
orange![]() |
c1 ![]() |
We can use exists logic here:我们可以在这里使用存在逻辑:
SELECT f1.fruit, f1.fruit_id
FROM fruits f1
WHERE EXISTS (
SELECT 1
FROM fruits f2
WHERE f2.fruit_id = f1.fruit_id AND
f2.fruit <> f1.fruit
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.