OhioWoodWright
asked on
SQL Join Problem
I have a number of tables I'm trying to join 2 of which are:
TDrivers
intDriverID
strFirstName
strMiddleName
strLastName
strAddress
strCity
strState
strZipCode
strPhoneNumber
TScheduledRoutes
intScheduledTimeID
intRouteID
intBusID
intDriverID
intAlternateDriverID
I am trying to join both TScheduledRoutes.intDriver ID and TScheduledRoutes.intAletne tnateDrive rID back to TDrivers in order to get the driver information and I'm not able to make it happen. the script for the join is:
TDrivers
intDriverID
strFirstName
strMiddleName
strLastName
strAddress
strCity
strState
strZipCode
strPhoneNumber
TScheduledRoutes
intScheduledTimeID
intRouteID
intBusID
intDriverID
intAlternateDriverID
I am trying to join both TScheduledRoutes.intDriver
SELECT
TR.intRouteID
,TR.strRoute
,TR.strRouteDescription
,TST.intScheduledTimeID
,TST.strScheduledTime
,TB.intBusID
,TB.strBus
,TB.intCapacity
,TD.intDriverID
,TD.strLastName
+ ', '
+ TD.strFirstName AS strDriver
,TD.intDriverID as intAlternateDriverID
,TD.strLastName
+ ', '
+ TD.strFirstName AS strAlternateDriver
FROM
TRoutes AS TR
,TScheduledRoutes AS TSR
,TScheduledTimes AS TST
,TBuses AS TB
,TDrivers AS TD
WHERE
TR.intRouteID = TSR.intRouteID
AND TSR.intScheduledTimeID = TST.intScheduledTimeID
AND TSR.intBusID = TB.intBusID
AND TSR.intDriverID = TD.intDriverID
AND TSR.intAlternateDriverID = TD.intDriverID
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks Ryan, may need to deal with NULLS coming from those left joins but looks good.
It also suggests this for your FROM clause:
Open in new window
The OR condition might not work, and if that is true then you might need to join the drivers table twice something like this: (and note the LEFT JOIN)Open in new window
Only with access to the tables and some data could I be more definite