Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

sql server - How SQL do in like?

I'd like do multiple like in query, the logic like below.

E.g:
Table1:

ID
AA001BB
AA002BB
AA003CC
AA004CC
AA004DD
AA006DD

Table2

ID
001
002
004

SQL Query Logic like:

select * from table1 where ID likein (select '%'+ID+'%' from table2)

Get result:

ID
AA001BB
AA002BB
AA004CC
AA004DD

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

as @Squirrel mention in comment, you can use EXISTS :

SELECT * FROM Table1 t1 WHERE 
EXISTS(SELECT * FROM Table2 t2 WHERE t1.id LIKE ('%' + t2.id + '%'))

here is db<>fiddle for better examine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...