-2

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.

jarlh
  • 42,561
  • 8
  • 45
  • 63
Eduardo
  • 508
  • 6
  • 18

4 Answers4

3

You are doing a cross product of the two tables. You need to restrict the number of results using some JOIN criteria.

Read more on this here: http://www.w3schools.com/sql/sql_join.asp

Adi
  • 4,149
  • 4
  • 25
  • 41
1

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%'
Rahul
  • 76,197
  • 13
  • 71
  • 125
0
select * from table_a TA, table_b TB where TB.number = '1447'  
UNION ALL 
select * from table_a TA, table_b TB where TA.subject like '%1447%'
Mansoor
  • 4,061
  • 1
  • 17
  • 27
0

Seems like the two table are more or less unrelated. Do a UNION ALL instead of JOIN:

select * from table_a TA where TA.subject like '%1447%'
UNION ALL 
select * from table_b TB where TB.number = '1447'  
jarlh
  • 42,561
  • 8
  • 45
  • 63