簡體   English   中英

像另一個表一樣的sql

[英]sql like with another table

例

我有兩個表,第一個有:value1,value2,value3第二個:我有完整的地址

我該如何使用查詢:

select * from table2 where address like '%table1.value1%table1.value2%table1.value3%'

我認為exists是您想要的:

select t2.*
from table2 t2
where exists (select 1
              from table1 t1
              where t2.address like concat('%', t1.value1, '%', t1.value2, '%', t1.value3, '%')
             );

就是說,需要這樣做表明您的數據模型有問題。

你可以試試看嗎?

select * from table2 where address like '%table1.value1%' OR address like '%table1.value2%' OR address like '%table1.value3%'

但是我認為您的數據模型應該規范化(2NF和3NF)。

REGEXP可用於將table1的內容與table2進行匹配。

select address, value1, value2, value3
from table1, table2 
where 
address REGEXP value1 and
address REGEXP value2 and
address REGEXP value3;

我覺得,如果需要匹配硬編碼值,最好使用like

select address from table1 where address like '%staticvalue%'

db-fiddle上查看一個實現的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM