我正在實施車輛管理系統。我有兩個不同的疑問,一個是提取一個機構的所有車輛,另一個是提取司機。每輛車都有一個分配給它的駕駛員。 現在我想合併這兩個查詢來返回車輛詳細信息以及分配給車輛的驅動程序的詳細信息。MS SQL合併兩個查詢以獲得一個結果
1查詢來獲取司機和他們的名字:
SELECT UserInfo.Name
FROM DriverInfo INNER JOIN UserInfo ON DriverInfo.Email = UserInfo.Email
INNER JOIN Agency ON UserInfo.AgencyID = Agency.AgencyID
WHERE (UserInfo.AgencyID = 1)
ORDER BY DriverInfo.DriverId DESC
第二查詢來獲取車輛詳細信息:
SELECT Vehicle.VehicleRegNum AS [Registration Number],
Vehicle.EngineNum AS [Engine Number],
Vehicle.FileRef AS [File Refrence],
Type.Name AS Type,
Make.Name AS Make ,
Vehicle.DriverID AS DriverID
FROM Vehicle INNER JOIN Type ON Vehicle.Type = Type.TypeId
INNER JOIN Make ON Vehicle.Make = Make.MakeId
WHERE (Vehicle.Approve = 1)
AND (Vehicle.AgencyId = 1)
AND (Vehicle.Maintained = 1)
ORDER BY Vehicle.VId DESC
在此查詢中,我得到了DriverID 並在DriverInfo:
DriverId, LicenseType, AppointmentDate, LicenseExpiryDate, ContractExpiryDate, Email
如何是這兩個相關?什麼是桌子結構?我沒有看到任何可用於將一個查詢與另一個查詢關聯的列。 – TTeeple
@Tepleple看到更新的問題 –
請提供樣本輸入和樣本輸出您的問題仍然不清楚 –