0
我想要結合這兩個查詢。SQL - 結合兩個查詢
查詢1 - 這告訴我我的清單中有所有車輛。含義不在所有權表中:
SELECT VEHICLE.*
FROM VEHICLE
WHERE NOT EXISTS
(SELECT NULL FROM OWNERSHIP WHERE VEHICLE.VEH_ID= OWNERSHIP.VEH_ID);
查詢2 - 這個告訴我哪個車是每個品牌的最高價格。
SELECT B.BRAND_ID, B.BRAND_NAME, M.MODEL_NAME, C.CLASS_NAME, V.VEH_ID, V.VEH_YEAR, V.VEH_PRICE
FROM (((VEHICLE AS V INNER JOIN CLASS AS C ON V.CLASS_ID = C.CLASS_ID)
INNER JOIN MODEL AS M ON M.MODEL_ID = V.MODEL_ID)
INNER JOIN BRAND AS B ON B.BRAND_ID = M.BRAND_ID)
INNER JOIN (SELECT M.BRAND_ID, MAX(V.VEH_PRICE) AS VEH_PRICE FROM VEHICLE AS V
INNER JOIN MODEL AS M ON M.MODEL_ID = V.MODEL_ID GROUP BY M.BRAND_ID)
AS derived ON (v.VEH_PRICE = derived.VEH_PRICE) AND (b.BRAND_ID = derived.BRAND_ID)
ORDER BY 7 DESC;
我意識到,確定哪些車輛是最昂貴的車輛是無關緊要的,如果他們已經被客戶所有。由於第一個查詢告訴我們哪些是可用的,我將如何結合這兩個?
還有另外一個'Vehicle AS V'條款,你沒有'修復',但我認爲這不會對結果產生影響。 –