mysql中三个表之间的内连接

时间:2012-02-15 12:10:52

标签: mysql join inner-join

我有3个名为

的表
  • 公司
  • 联系人
  • 广告活动

联系人拥有公司的外键。公司有外交活动的关键。所有这些都有一个列'名称'。我需要一个联系人表,其中包含联系人姓名公司名称和活动名称。

contact
id   name company_id
companies
id   name campaign_id
campaigns
id   name company_id

2 个答案:

答案 0 :(得分:6)

select con.name as contact_name , com.name as company_name,campa.name as campaign_name
from contact con inner join company com
on con.companyid = com.companyid
inner join campaign campa
on com.campaignid = campa.campaignid

假设受尊重表的id列

答案 1 :(得分:2)

SELECT contacts.name,companies.name,campaigns.name 
FROM contacts 
INNER JOIN 
(companies INNER JOIN campaigns 
ON campaigns.campaignid=companies.campaignid) 
ON contacts.companyid=companies.companyid
AND //add any additional filters HERE

希望这很有帮助