如果一列中的值存在于另一列中,则过滤行 [英] Filter rows if value in one column exists in another column

查看:58
本文介绍了如果一列中的值存在于另一列中,则过滤行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Postgres 11中有下表:

I have following table in Postgres 11:

col1   col2            col3               col4   
1      trial_1         ag-270              ag
2      trial_2         ag                  ag
3      trial_3         methotexate (mtx)   mtx
4      trial_4         mtx                 mtx
5      trial_5         hep-nor-b           nor-b

我想在col3列中搜索col4的每个值.如果col3中存在col4中的值,我想保留这些行,否则应将该行排除在外.

I would like to search each value of col4 throughout the column col3. If the value in col4 exists in col3, I would like to keep the rows else the row should be excluded.

所需的输出是:

col1   col2            col3               col4   
1      trial_1         ag-270              ag
2      trial_2         ag                  ag
3      trial_3         methotexate (mtx)   mtx
4      trial_4         mtx                 mtx

我无法对此进行任何尝试,因为我还无法找到解决方案.

I could not try anything on this as I am unable to find a solution to this yet.

推荐答案

这可以作为内部联接来完成:

This can be done as an inner join:

select distinct t.col1, t.col2, t.col3, t,col4
from T t inner join T t2 on t2.col3 = t.col4 

这篇关于如果一列中的值存在于另一列中,则过滤行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆