mysql加入1:2关系

时间:2015-05-22 07:22:13

标签: mysql join relation

我正坐在那里思考一个1:2关系问题的解决方案: 2个表(更复杂,但源问题更容易理解)。

表1机器

id | name | location_id | manufacturar_id | ...

表2合作伙伴

id | address | name | firstname | companyname | ...

我想得到像:

jigsaw xpy123 | super-carpenter, mainstreet 12, (location, partner-table) | GE wood servcies, x-street, Maine (manufacturer)

基本上,它应该是两个查询:

select handelspartner.name as hersteller 
from handelspartner,anlagen 
where anlagen.hersteller_id=handelspartner.id

select handelspartner.name as standort 
from handelspartner,anlagen 
where anlagen.standort_id=handelspartner.id

但是在一个查询中这应该是某种可能的吗?

1 个答案:

答案 0 :(得分:0)

对表handelspartner使用别名。

select handelspartner1.name as hersteller, handelspartner2.name as standort 
from anlagen 
join handelspartner handelspartner1 on anlagen.hersteller_id=handelspartner1.id
join handelspartner handelspartner2 on anlagen.standort_id=handelspartner2.id