Eduardo Eduardo - 9 days ago 6
SQL Question

SQL where term is equals or like (two tables)

With MySQL, I need to find all records where a column is like a term OR the term is equals a column but in another table.

For example:

select *
from table_a TA,
table_b TB
where TB.number = '1447'
or TA.subject like '%1447%'


In practice, I'm searching for a value, it can be on TB.number OR TA.subject. This SQL above is returning multiple records and it not matches the search.

Answer

Yeah, you need to do a JOIN among the table on a common column like

select * 
from table_a TA
join table_b TB 
on TA.id = TB.id
where TB.number = '1447' 
or TA.subject like '%1447%'