简体   繁体   English

如何在多个表中搜索关键字以及如何在其他表中搜索关系

[英]How to search keywords over multiple tables and search relations in other table

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?
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%';

I think i found the right solution, with a little help from ANKIT. 我想我在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM