Mysql SQL:结果集中1中有2列
假设我有3张桌子: 信息\u联系人:Mysql SQL:结果集中1中有2列,mysql,sql,join,Mysql,Sql,Join,假设我有3张桌子: 信息\u联系人: id_contact id_demand email 1 1 contact1@a.com 2 2 contact2@a.com 3 3 contact3@a.com id_demand date 1 2016-10-12 2
id_contact id_demand email
1 1 contact1@a.com
2 2 contact2@a.com
3 3 contact3@a.com
id_demand date
1 2016-10-12
2 2016-11-05
3 2016-12-12
id_invitation id_demand partner_company concurrent_company
1 1 google facebook
2 1 null linkedin
3 2 google null
4 2 null yahoo
5 3 google null
需求:
id_contact id_demand email
1 1 contact1@a.com
2 2 contact2@a.com
3 3 contact3@a.com
id_demand date
1 2016-10-12
2 2016-11-05
3 2016-12-12
id_invitation id_demand partner_company concurrent_company
1 1 google facebook
2 1 null linkedin
3 2 google null
4 2 null yahoo
5 3 google null
邀请:
id_contact id_demand email
1 1 contact1@a.com
2 2 contact2@a.com
3 3 contact3@a.com
id_demand date
1 2016-10-12
2 2016-11-05
3 2016-12-12
id_invitation id_demand partner_company concurrent_company
1 1 google facebook
2 1 null linkedin
3 2 google null
4 2 null yahoo
5 3 google null
我希望得到这样的结果:
Company | id_demand
----------------------
Facebook | 1
Google | 1
Google | 2
Google | 3
Linkedin | 1
Yahoo | 2
合作伙伴公司和并发公司之间没有差异(一起计算结果)
目前,我已尝试:
SELECT i.partner_company, d.id_demand
FROM info_contact as c, demand as d, invitation as i
WHERE c.id_demand = d.id_demand AND d.id_demand = i.id_demand
AND i.partner_company IS NOT NULL
GROUP BY i.partner_company, d.id_demand;
及
我不知道如何组合这两个查询并获得我想要的结果尝试使用
UNION ALL
select partner_company , id_demand
From invitation
Where partner_company is not null
Union All
select concurrent_company , id_demand
From invitation
Where concurrent_company is not null
另外,我没有加入其他表,因为您没有选择它们。如果要检查是否存在,请加入它。使用
内部联接
语法联接表尝试使用联合所有
select partner_company , id_demand
From invitation
Where partner_company is not null
Union All
select concurrent_company , id_demand
From invitation
Where concurrent_company is not null
另外,我没有加入其他表,因为您没有选择它们。如果要检查是否存在,请加入它。使用内部联接
语法联接表