我有这两个我正在尝试组合的查询。
查询 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;
我意识到,如果它们已经由客户拥有,那么确定哪些车辆是最昂贵的并不重要。由于第一个查询告诉我们哪些可用,我将如何将这两者结合起来?