简体   繁体   English

如何比较和提取 2 列 PostgreSQL

[英]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水果ID
apple苹果 a1 a1
banana香蕉 b1 b1
citrus柑橘 c1 c1
manggo芒果 a1 a1
orange c1 c1

I expected the result is:我预计结果是:

fruit水果 fruit_id水果ID
apple苹果 a1 a1
manggo芒果 a1 a1
citrus柑橘 c1 c1
orange c1 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM