Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: expading "in (...)" to "like"

Tags:

sql

I have a sql select like below:

select * from table1 where text in (select text from table2)

In real in in clausule is more complex select that this. text is string (varchar). How to expand that sql to select rows from table1 where text is like texts from table2 (not only exactly equals) ?

like image 429
marioosh Avatar asked Jan 19 '26 22:01

marioosh


1 Answers

If you have your wild card expressions in the text column in Table2 you can do like this.

select *
from Table1 as T1
where exists (select *
              from Table2 as T2
              where T1.[text] like T2.[text])

Otherwise you need to add the % in your query.

select *
from Table1 as T1
where exists (select *
              from Table2 as T2
              where T1.[text] like '%'+T2.[text]+'%')
like image 167
Mikael Eriksson Avatar answered Jan 22 '26 14:01

Mikael Eriksson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!