0

我的查询中的同一列上有多个类似的子句。是否可以在结果集中包含类似的子句值?例如

SELECT * FROM address ADDR WHERE 
addr.line LIKE '%ONE%' 
OR addr.line LIKE '%TWO%'
OR addr.line LIKE '%THREE%'
OR addr.line LIKE '%FOUR%';

结果集包含所有地址,包括一、二、三、四。现在我想看看结果集

Address1ONE | ONE
Address2TWO | TWO
THREEaddr   | THREE

任何线索?-R

4

1 回答 1

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%';
于 2013-07-05T23:18:59.640 回答