简体   繁体   中英

Join three tables mysql

I have three tables of vendor details, services and venues. All has a common field as vendor 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. 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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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