SQL小提琴
Oracle 11g R2 模式设置:
CREATE TABLE ABBREVIATIONS(
Short VARCHAR2(10) CONSTRAINT ABBR__S__PK PRIMARY KEY
CONSTRAINT ARRB__S__CHK CHECK( Short = UPPER( Short ) ),
Value VARCHAR2(100) CONSTRAINT ABBR__V__U UNIQUE
CONSTRAINT ABBR__V__CHK CHECK( Value IS NOT NULL AND Value = UPPER( Value ) )
);
INSERT INTO Abbreviations VALUES ( 'XYZ', 'XAVIER YELLOW ZEBRA' );
CREATE TABLE Tests ( Col1, Col2, Col3 ) AS
SELECT 1, 'XYZ', 'XYZ' FROM DUAL
UNION ALL SELECT 2, 'xyz', 'XYZ' FROM DUAL
UNION ALL SELECT 3, 'XYZ', 'xyz' FROM DUAL
UNION ALL SELECT 4, 'xyz', 'xyz' FROM DUAL
UNION ALL SELECT 5, 'xyz', 'abcdXYZ' FROM DUAL
UNION ALL SELECT 6, 'xyz', 'XYZefg' FROM DUAL
UNION ALL SELECT 7, 'xyz', 'ghiXYZjkl' FROM DUAL
UNION ALL SELECT 8, 'xyz', 'XaViEr YelloW ZeBrAXXYYZZ' FROM DUAL
UNION ALL SELECT 9, 'Xavier Yellow Zebra', 'XXYZZ' FROM DUAL
UNION ALL SELECT 10, 'xyz', 'xy -- Not a match -- z' FROM DUAL;
查询 1:
SELECT *
FROM Tests t
WHERE UPPER( Col3 ) LIKE '%' || UPPER( Col2 ) || '%'
OR EXISTS (
SELECT 'X'
FROM ABBREVIATIONS a
WHERE ( UPPER( t.col2 ) = a.Short
OR UPPER( t.Col2 ) = a.Value )
AND REPLACE( UPPER( Col3 ), a.Short, a.Value ) LIKE '%' || a.Value || '%'
)
结果:
| COL1 | COL2 | COL3 |
|------|---------------------|---------------------------|
| 1 | XYZ | XYZ |
| 2 | xyz | XYZ |
| 3 | XYZ | xyz |
| 4 | xyz | xyz |
| 5 | xyz | abcdXYZ |
| 6 | xyz | XYZefg |
| 7 | xyz | ghiXYZjkl |
| 8 | xyz | XaViEr YelloW ZeBrAXXYYZZ |
| 9 | Xavier Yellow Zebra | XXYZZ |