简体   繁体   English

使用子集表中的键对对原始 sql 表进行子集化

[英]Subsetting the original sql table using key pairs from a subset table

I have the following table:我有下表:

CREATE TABLE test (key1 varchar(10), key2 varchar(10), col3 varchar(10), 
                   col4 varchar(10), col5 varchar(10), val varchar(10))

INSERT INTO test VALUES('A', 'B', 'C', 'D', 'F', 'good1')
INSERT INTO test VALUES('F', 'C', 'C', 'D', 'F', 'bad1')
INSERT INTO test VALUES('A', 'D', 'C', 'D', 'F', 'good2')
INSERT INTO test VALUES('N', 'B', 'C', 'D', 'F', 'bad2')
INSERT INTO test VALUES('A', 'B', 'C', 'D', 'F', 'want this')
INSERT INTO test VALUES('A', 'D', 'C', 'D', 'F', 'and this')

I would like to extract key1, key2 pairs that where val column includes "good".我想提取val列包含“good”的 key1, key2 对。 Then I need to go back to the original table and extract all the entries where the mentioned key1, key2 pairs appear.然后我需要返回到原始表并提取提到的 key1、key2 对出现的所有条目。

I am using the following:我正在使用以下内容:

select t.* from test t where exists (select 1
              from t t2
              where t2.value like '%good%') and
              t2.key1 = t.key1 and
              t2.key2 = t.key2)

Upon running the above, I get the following error:运行上述程序后,我收到以下错误:

"Object t does not exist" “对象 t 不存在”

You need to refer to the table as test , not t :您需要将该表称为test ,而不是t

select t.*
from test t
where exists (select 1
              from test t2
              where t2.value like '%good%') and
                    t2.key1 = t.key1 and
                    t2.key2 = t.key2
              );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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