2017-09-14 26 views
1

我想創建一個查詢,其中包含子查詢的結果所有用戶名。我已經寫了下面的一些僞代碼:過濾表中字段像子查詢的結果

select <field1> 
from <table1> 
where <field> like 
    (select <field2> from <table2>) 

我基本上是想用我的查詢與結束:

table1.field1 like '%' || table2.field2 || '%' 

這可能嗎?

+0

你可以「連接」表格並指定「like」條件。 –

+0

嗯,你可以寫'where like ANY(選擇'%'|| table2.field2 ||'%'from )',這將導致類似於Gordon的exists的解釋。希望至少有一個表很小,否則產生的* Product Join *效率會非常低。 – dnoeth

+0

在SQL世界表中有_columns_,而不是_fields_。 – jarlh

回答

2

使用exists

select <field1> 
from <table1> t1 
where exists (select <field2> 
       from <table2> t2 
       where t1.field1 like '%' || t2.field2 || '%' 
      ); 

你可能也有join做到這一點,但你可能要以後刪除重複。

+0

工作過一個魅力,謝謝! – Nic2352