如何在多个表上搜索关键字并在其他表中搜索关系

时间:2017-05-17 10:15:33

标签: php mysqli

I have some huge tables, company's and contact's

table: t_company
id  company_name(fulltext)
1   rowa bv
2   jordan coll
3   AH
4   Vries BV

table: t_contact(fulltext)
id  fullname
1   johan de vries
2   rowa hobbeman
3   jacob schoon
4   anna schoon

// this table to link contacts with company's. contacts can be linked to multiple company's 
table: company_contact
id  id_ref_company  id_ref_contact
1   1       2
2   2       1
3   2       4
4   3       1
5   4       2


i like to search t_company.company_name and t_contact.fullname with keyword search 
When its found in t_company then look in company_contact.id_ref_contact for contacts 
When its found in t_contact then look in company_contact.id_ref_company for companys 

example search: 'de vries' match t_contact.id 1 and t_company.id 4
found in t_contact so get from company_contact the id_ref_company id's
found in t_company so get from company_contact the id_ref_contact id's
result should be
3 results:
t_contact.id    t_contact.fullname  t_company.id    t_company.company_name
1       johan de vries      2       jordan coll
1       johan de vries      3       AH
2       anna schoon     4       Vries BV

is it possible to do this in one query?

2 个答案:

答案 0 :(得分:1)

Select c.id, c.fullname, a.id, a.company_name
from t_company a
join company_contact b on a.id = b.id_ref_company
join t_contact c on c.id = b.id_ref_contact
where c.fullname like '%de vries%';

答案 1 :(得分:0)

我想在ANKIT的帮助下找到了正确的解决方案。

Select c.id, c.fullname, a.id, a.company_name 
from t_company a 
join company_contact b on a.id = b.id_ref_company 
join t_contactpersoonpersoon c on c.id = b.id_ref_contact 
WHERE MATCH(c.fullname) AGAINST ('de* +vries*' IN BOOLEAN MODE) or 
MATCH(a.company_name) AGAINST ('de* +vries*' IN BOOLEAN MODE)