goofyui goofyui - 3 months ago 13
SQL Question

How to write a SQL like query with table column?

How to write a SQL like query with table column ?

I have two tables table1 and table2.

Table1 has Notes
Table2 has billid


We have around 100 bills which satisfies Like condition.

Right now, i am writing with Static value on the column.

How should i write a Like Query by passing Column in the Like Condition.

select * from table1
where Notes like select billid from table2

Answer

You are wanting a JOIN here.

If the exact BillID is found in Table1.Column1 then:

select table1.*
from table1 
    INNER JOIN table2 ON
        table1.column1=table2.billid

You could also do this using IN in the WHERE clause if you truly don't need any records from your table2 in the result set:

select *
from table1
where column1 in (SELECT billid FROM table2);

Which is pretty close to what you were aiming for in your attempted query.

Lastly, if you actually mean LIKE which is more of a wildcard match in SQL terms where the column1 merely has to contain a billid then back to the join:

select table1.*
from table1 
    INNER JOIN table2 ON
        table1.column1 LIKE '%' + table2.billid + '%'
Comments