我有3张不同的桌子
1. Table Business
ID, Name, fk_Input, fk_Output
2. Table Input
Id, FK_Business_id, Name
3.
Id, FK_Business_id, Name
我在SQLFiddle上创建了一个数据库示例。
现在只想检查业务是否具有相同的output.name,就像其他一些Business具有Input.name并显示为结果,可以共享商业名称和资源。
这是一个更好理解的例子
1. Table Business
ID, Name, fk_Input, fk_Output
1, AdventureA, 1,1
2, BuinsnessB, 2,2
3, CompanyC, 3,3
2.Table Input
Id, FK_Business_id, Name
1, 1(AdventureA),coal
2, 1(AdventureA),Air
3, 2(BusinessB) ,Cooper
4, 2(BusinessB) ,Power
5, 3(CompanyC) ,Wood
3.Table Output
Id, FK_Business_id, Name
1, 1(AdventureA), Power
2, 2(BusinessB) , Cooperbar
3, 3(CompanyC) , Power
结果应该是:
AdventureA has Power for BusinessB
CompanyC has Power for BusinessB
或者换句话说就像这样
BusinessnameOutput, BusinessnameInput, NameInput, NameOutput
我试过了
SELECT DISTINCT Business.Name, Output.Name, Input.Name FROM Business, Input, Output
WHERE Output.Name = Input.Name
但导致很多行没有任何意义
也尝试了
SELECT Business.Name, Input.Name, Output.Name
FROM Unternehmen
INNER JOIN Input ON Input.FK_Business_id, = Business.ID
INNER JOIN Output ON Output.FK_Business_id = Business.ID
WHERE Input.Name = Output.Name
但不会导致任何条目。
查询几乎正确的结果是
SELECT Business.Name, Input.Name, Output.Name FROM Business,Input, Output WHERE Input.Name = Output.Name GROUP BY Input.RessourceName
但它只显示一个Businessname,而不是两个同时具有相同输出的商业名称。
我不知道如何解决这个难题?任何人都有了想法,那会很棒。
答案 0 :(得分:1)
您可以使用内部联接和适当的条件进行加入
select distinct concat( Business.Name, ' has ', Output.Name, ' for ', Business2.name)
from Business
inner join Output on Business.fk_Output = Output.id
inner join Input on Input.name = Output.name
inner join Business as Business2 on Input.fk_business_id = Business2.id ;