2016-11-28 78 views
-2

对于MySQL,我需要找到所有记录,其中一列就像一个术语,或者这个术语等于一列,但在另一个表中。SQL where term is equals or like(two tables)

例如:

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

在实践中,我在寻找一个价值,它可以在TB.number或TA.subject。上面的这个SQL返回多条记录,它不匹配搜索。

+1

请告诉我姐姐'table_a'&'table_b'之间的公共列? – Vikrant

回答

1

是的,你需要做一个JOIN表中的公共列像

select * 
from table_a TA 
join table_b TB 
on TA.id = TB.id 
where TB.number = '1447' 
or TA.subject like '%1447%' 
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%' 
0

好像两个表都比较或不太相关。做的UNION ALL代替JOIN

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