2016-11-28 68 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'