![](/img/trans.png)
[英]SQL selecting only rows from 2nd table, containing exact results received from 1st table
[英]SQL Search rows that contain strings from 2nd table list
我有一個主表,其中包含要搜索的字符串列表。 如果單元格中的任何字符串包含主查找表中的文本,則返回TRUE / FALSE。 目前,我使用的是excel
=SUMPRODUCT(--ISNUMBER(SEARCH(masterTable,[@searchString])))>0
有沒有辦法在SQL中做這樣的事情? 如果字符串相等,則LEFT JOIN或OUTER APPLY將是簡單的解決方案。 但它們需要包含。
SELECT *
FROM t
WHERE col1 contains(lookupString,lookupColumn)
--that 2nd table could be maintained and referenced from multiple queries
hop
bell
PRS
2017
我期望的結果是如果該行包含查找表中的任何字符串,則該列顯示TRUE / FALSE
SEARCH_STRING Contained_in_lookup_column
hopping TRUE
root FALSE
Job2017 TRUE
PRS_tool TRUE
hand FALSE
抱歉,我現在無法訪問數據庫來確認語法,但是應該是這樣的:
SELECT t.name,
case when (select count(1) from data_table where data_col like '%' || t.name || '%' > 0) then 'TRUE' else 'FALSE' end
FROM t;
要么
SELECT t.name,
case when exists(select null from data_table where data_col like '%' || t.name || '%') then 'TRUE' else 'FALSE' end
FROM t;
塞爾吉奧
您可以將%
通配符與LIKE
和EXISTS
結合使用。
示例(使用Oracle語法)-我們有一個包含數據的v_data表和一個包含查詢詞的v_queries表:
with v_data (pk, value) as (
select 1, 'The quick brown fox jumps over the lazy dog' from dual union all
select 2, 'Yabba dabba doo' from dual union all
select 3, 'forty-two' from dual
),
v_queries (text) as (
select 'quick' from dual union all
select 'forty' from dual
)
select * from v_data d
where exists (
select null
from v_queries q
where d.value like '%' || q.text || '%');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.