简体   繁体   中英

Selecting multiple rows in same table in the same JOIN

In the picture is my table situation right now:

表 The central table in this case right now is tblJob , here is everything defined what I need (not all in the picture).

The address table needs to return 2 values (1 of the company and 1 of the job itself). The only thing I need to do right now is to add the company address (the job address is already in my query) My query already looks like this:

SELECT
tblJob.jobID,
tblJob.amount        AS jobAmount,
tblJob.extraInfo     AS jobExtraInfo,
tblJob.views         AS jobViews,
tblJob.description   AS jobDescription,
tblJob.dateCreated   AS jobDateCreated,
tblJobFunction.jobFunctionID,
tblJobFunction.jobFunction,
tblAddress.zipcode   AS jobAddress,
tblAddress.city      AS jobCity,
tblAddress.street    AS jobStreet,
tblAddress.number    AS jobNumber,
tblAddress.bus       AS jobBus,
tblCountry.countryID AS jobCountryID,
tblCountry.country   AS jobCountry,
tblCountry.areaCode  AS jobAreaCode,
tblCompany.companyID,
tblCompany.name,
tblCompany.email,
tblCompany.GSM,
tblCompany.phoneNumber,
tblCompany.photoURL  AS companyPhotoURL,
tblCompany.VATNumber,
tblCompany.websiteURL,
tblEvent.eventID,
tblEvent.event,
tblEvent.description AS eventDescription,
tblEvent.startDate   AS eventStartDate,
tblEvent.endDate     AS eventEndDate,
tblEvent.facebookURL,
tblEvent.photoURL    AS eventPhotoURL,
tblEvent.views       AS eventViews,
tblEvent.dateCreated AS eventDateCreated
FROM tblJob
JOIN tblAddress ON tblAddress.addressID = tblJob.addressID
JOIN tblCountry ON tblAddress.countryID = tblCountry.countryID
JOIN tblJobFunction ON tblJob.jobFunctionID = 
                       tblJobFunction.jobFunctionID
JOIN tblCompany ON tblJob.companyID = tblCompany.companyID
LEFT JOIN tblEvent ON tblJob.eventID = tblEvent.eventID

Now the question is: how can I add the address from the company in the same query?

Use the address table as many times as you need it, but each time you must give it a new alias:

FROM tblJob
JOIN tblAddress ON tblAddress.addressID = tblJob.addressID
JOIN tblCountry ON tblAddress.countryID = tblCountry.countryID
JOIN tblJobFunction ON tblJob.jobFunctionID =  tblJobFunction.jobFunctionID
JOIN tblCompany ON tblJob.companyID = tblCompany.companyID

JOIN tblAddress a2 ON a2.addressID = tblCompany.addressID

LEFT JOIN tblEvent ON tblJob.eventID = tblEvent.eventID

perhaps more like this:

SELECT .street, .street
FROM tblJob
JOIN tblAddress  ON .addressID = tblJob.addressID
JOIN tblCompany ON tblJob.companyID = tblCompany.companyID
JOIN tblAddress  ON .addressID = tblCompany.addressID

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