简体   繁体   English

加入三张表的mysql

[英]Join three tables mysql

I have three tables of vendor details, services and venues. 我有供应商详细信息,服务和地点的三个表格。 All has a common field as vendor id. 全部都有一个公共字段作为供应商ID。

I want to show the services and venues related to the vendor. 我想展示与供应商有关的服务和场所。

I tried some joins but not getting the result. 我尝试了一些联接,但没有得到结果。

one of them I tried is : 我尝试过的其中之一是:

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

but it shows the error for services.serviceId as a unknown column. 但它会以未知列的形式显示services.serviceId的错误。 I checked the column does exist in services table. 我检查了服务表中是否确实存在该列。

How can I get this? 我怎么能得到这个? Thank you.. 谢谢..

Actually you were using table alias as v s venue for the tables vendorDetails services venues respectively. 其实你用表的别名为v s venue为表vendorDetails services venues分别。 Therefore the corresponding tablename were replaced by their alias for this particular query. 因此,针对此特定查询,相应的表名已被其别名替换。 Hence the original table name become unknown. 因此,原始表名变得未知。 And in inner join venues venue on v.vendorId = venues.venueId you are comparing vendorId with venueId which will return empty result set. venue on v.vendorId = venues.venueId场馆venue on v.vendorId = venues.venueId inner join场馆venue on v.vendorId = venues.venueId您正在比较vendorIdvenueId ,这将返回空结果集。 Please try below query 请尝试以下查询

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM