加入三个表mysql

时间:2016-08-12 04:30:21

标签: mysql join multiple-tables

我有三张供应商详情,服务和场地表。所有人都有一个公共字段作为供应商ID。

我想展示与供应商相关的服务和场地。

我尝试了一些连接,但没有得到结果。

我试过的其中一个是:

SELECT services.serviceId as Id, 
services.service_name as service_name,
services.entry_by as entry_by,
services.servicetypeId as servicetypeId, 
services.latitude as latitude,
services.longitude as longitude, 
services.active as active, 

vendorDetails.username, 
vendorDetails.emailId, 
vendorDetails.vendorAddress,
vendorDetails.vendorName,
vendorDetails.mobileno, 

venues.venueId as Id, 
venues.venue_name as venue_name, 
venues.entry_by as entry_by, 
venues.venuetypeId as venuetypeId, 
venues.latitude as latitude,
venues.longitude as longitude,
venues.active as active 

FROM `vendorDetails` v

inner join `venues` venue on v.vendorId = venues.venueId
inner join `services` s on v.vendorId  = s.vendorId

但是它将services.serviceId的错误显示为未知列。我检查了列确实存在于services表中。

我怎么能得到这个?谢谢..

1 个答案:

答案 0 :(得分:0)

实际上,您分别使用表格v s venue作为表格vendorDetails services venues。因此,相应的表名由其特定查询的别名替换。因此原始表名称变得未知。在inner join场地venue on v.vendorId = venues.venueId中,您要将vendorIdvenueId进行比较,这将返回空结果集。请尝试以下查询

SELECT 
    s.serviceId as Id, 
    s.service_name as service_name,
    s.entry_by as entry_by,
    s.servicetypeId as servicetypeId, 
    s.latitude as latitude,
    s.longitude as longitude, 
    s.active as active, 

    v.username, 
    v.emailId, 
    v.vendorAddress,
    v.vendorName,
    v.mobileno, 

    venue.venueId as Id, 
    venue.venue_name as venue_name, 
    venue.entry_by as entry_by, 
    venue.venuetypeId as venuetypeId, 
    venue.latitude as latitude,
    venue.longitude as longitude,
    venue.active as active 

FROM 
    `vendorDetails` v

Inner Join
    `venues` venue on v.vendorId = venue.vendorId 
Inner Join 
    `services` s on v.vendorId  = s.vendorId