1

我正在编写一个存储过程,它将字符串作为输入参数之一,然后检索结果。

我熟悉以下 SQLCASE格式。

CASE
  WHEN col1 in ('val1', 'val2', 'val3')
  THEN 'HELLO'    
  WHEN col1 in ('val4', 'val5', 'val6') 
  THEN 'WORLD'    
END AS col2

其他方式的查询应该是什么?

就像我给col2value as 一样'HELLO',它应该返回所有col1包含的行'val1', 'val2', 'val3'

4

4 回答 4

2

只需将其包装在另一个查询中...

SELECT *
FROM (
      SELECT CASE
             WHEN col1 in ('val1', 'val2', 'val3')
             THEN 'HELLO'    
             WHEN col1 in ('val4', 'val5', 'val6') 
             THEN 'WORLD'    
             END AS col2
...
     )t
WHERE t.col2 = 'HELLO'
于 2019-12-11T20:08:03.683 回答
0

你可以试试这个。

SELECT
   col1,
   CASE
   WHEN col1 in ('val1', 'val2', 'val3')
   THEN 'HELLO'    
   WHEN col1 in ('val4', 'val5', 'val6') 
   THEN 'WORLD'    
   END AS col2
FROM
   --wherever you are getting your data from...
WHERE
   col2 = :SEARCH

当您将 HELLO 传递给绑定变量时,它将返回如下所示的结果。

col1 col2
---- ----
val1 HELLO
val2 HELLO
val3 HELLO

如果你只想返回 col1,你可以做这样的事情

WITH SEARCH_RESULTS AS
(
   SELECT
      col1,
      CASE
      WHEN col1 in ('val1', 'val2', 'val3')
      THEN 'HELLO'    
      WHEN col1 in ('val4', 'val5', 'val6') 
      THEN 'WORLD'    
      END AS col2
   FROM
      --wherever you are getting your data from...
   WHERE
      col2 = :SEARCH
)

SELECT col1 FROM SEARCH_RESULTS
于 2019-12-12T01:59:02.577 回答
0
WHERE 1 = CASE
  WHEN (col2 = 'HELLO' and col1 in ('val1', 'val2', 'val3'))
    OR (col2 = 'WORLD' and col1 in ('val4', 'val5', 'val6'))
  THEN 1
  ELSE 0
  END

或者,更简单地说..

WHERE  (col2 = 'HELLO' and col1 in ('val1', 'val2', 'val3'))
    OR (col2 = 'WORLD' and col1 in ('val4', 'val5', 'val6'))
于 2019-12-11T20:08:11.200 回答
0

其中 (col2 = 'HELLO' and col1 in ('val1', 'val2', 'val3')) 或 (col2 = 'WORLD' and col1 in ('val4', 'val5', 'val6'))

于 2019-12-11T20:09:08.790 回答