我在查询中的同一列上有多个like子句。 是否可以在结果集中包含like子句值? 例如
SELECT * FROM address ADDR WHERE
addr.line LIKE '%ONE%'
OR addr.line LIKE '%TWO%'
OR addr.line LIKE '%THREE%'
OR addr.line LIKE '%FOUR%';
结果集包含所有地址,包括ONE,TWO,THREE,FOUR。 现在我希望看到结果集如
Address1ONE | ONE
Address2TWO | TWO
THREEaddr | THREE
任何线索? -R
答案 0 :(得分:1)
SELECT addr.*,
CASE WHEN addr.line LIKE '%ONE%' THEN 'ONE'
WHEN addr.line LIKE '%TWO%' THEN 'TWO'
WHEN addr.line LIKE '%THREE%' THEN 'THREE'
WHEN addr.line LIKE '%FOUR%' THEN 'FOUR'
ELSE 'Unknown' -- redundant since other values filtered out by WHERE...
END AS address_type
FROM address ADDR WHERE
addr.line LIKE '%ONE%'
OR addr.line LIKE '%TWO%'
OR addr.line LIKE '%THREE%'
OR addr.line LIKE '%FOUR%';