就纯代码清洗而言,以下代码看起来更干净:
SELECT *
FROM foobar
WHERE (SUBJECT = 'STAT' and TERM IN ('111','222','666') )
OR (SUBJECT = 'COMM' and TERM IN ('333','444') )
OR (SUBJECT = 'ENGLISH' and TERM = '555' ) ;
根据应用程序和逻辑的重用频率,可能值得设置查找表以应用逻辑:
CREATE TABLE foobar_lookup (SUBJECT VARCHAR2(7), TERM VARCHAR2(3)) ;
INSERT INTO foobar_lookup SELECT 'STAT', '111' FROM dual ;
INSERT INTO foobar_lookup SELECT 'STAT', '222' FROM dual ;
INSERT INTO foobar_lookup SELECT 'STAT', '666' FROM dual ;
INSERT INTO foobar_lookup SELECT 'COMM', '444' FROM dual ;
INSERT INTO foobar_lookup SELECT 'COMM', '333' FROM dual ;
INSERT INTO foobar_lookup SELECT 'ENGLISH', '555' FROM dual ;
SELECT f.* FROM foobar f
JOIN foobar_lookup fl
ON fl.subject = f.subject
AND fl.term = f.term ;