要在 SQL 中执行此操作,我认为您需要合并分层查询。这是一个摇摆,但我无法测试它。
SELECT optionID, NVL( MAX(optionValue), MAX(optionDefaultValue) ) optionValue
FROM (SELECT optionID, optionDefaultValue, subscriptionID, inherits
FROM options CROSS JOIN subscriptions
WHERE optionID IN (a,b,c,d,e,f)
)
LEFT JOIN subscriptionOptions USING (optionId, subscriptionID)
START WITH subscriptionID = x
CONNECT BY PRIOR optionValue IS NULL
AND subscriptionID = PRIOR inherits
AND optionID = PRIOR optionID
GROUP BY optionID
另一种方法是编写一个函数,为单个 subscriptionID 和 optionID 实现递归逻辑,然后像这样调用它:
SELECT optionID, NVL( getSubscriptionOption( x, optionID), optionDefaultValue )
FROM options
WHERE optionID IN (a,b,c,d,e,f)
该函数可能类似于:
FUNCTION getSubscriptionOption( pSubID NUMBER, pOptID NUMBER )
RETURN subscriptionOptions.optionValue%TYPE
IS
l_optionValue subscriptionOptions.optionValue%TYPE;
l_inherits subscriptionOptions.inherits%TYPE;
BEGIN
SELECT optionValue
INTO l_optionValue
FROM subscriptionOptions
WHERE subscriptionID = pSubID
AND optionID = pOptID;
RETURN l_optionValue;
EXCEPTION
WHEN no_data_found THEN
SELECT inherits
INTO l_inherits
FROM subscriptions
WHERE subscriptionID = pSubID;
IF inherits IS NULL THEN
RETURN NULL;
ELSE
RETURN getSubscriptionOption( l_inherits, pOptID );
END IF;
END;
或者可以写成使用循环而不是递归。