加入多个mysql表

时间:2017-12-11 02:01:32

标签: mysql join

我在数据库中有三个表:makerspartstransfers

Makersmakerid,stampname

Partscategories,name

variationsid, provider, recipient, part

Provider是指向makers/makerid的指针,因此recipients

我需要查询,以便我的结果显示友好名称与编码/简化数据库ID。

我尝试了几个连接,但我的查询:

select makers.stampname as giver, makers.stampname as takers
from makers
   left join variations prov on prov.provider=makers.makerid
   left join variations recp on recp.recipient=makers.makerid limit 5;

在两列中生成相同的名称。如果providermakerid 24且recipientmakerid 10,我如何获取查询以回复来自{{1}的重合stampname db?

1 个答案:

答案 0 :(得分:0)

抱歉,看起来需要更好地理解别名。这非常有效:

select variations.date,provider.stampname as provname ,
recipient.stampname as recipname, 
parts.name from variations 
left outer join 
makers provider on variations.provider = provider.makerid 
left outer join 
makers recipient on variations.recipient = recipient.makerid
left join categories parts on variations.part = parts.catid 
where variations.provider = "20" 
and 
variations.date="1944";