SQL查询的WHERE子句中的CASE语句

时间:2014-08-20 07:37:48

标签: mysql sql oracle11g

我想编写一个根据条件在运行时构造WHERE子句的查询。这就是我想要做的。

SELECT name from data_table WHERE 
CASE
WHEN input_data = location THEN <where condition should be based on location>
WHEN input_data = college  THEN <where condition should be based on college name>

我该怎么做呢?有可能吗?

2 个答案:

答案 0 :(得分:1)

SELECT name from data_table
WHERE (input_data = location AND condition based on location)
   OR (input_data = college  AND condition based on college name)

答案 1 :(得分:0)

试试这个:

SELECT name 
  from data_table 
 WHERE 
   CASE
     WHEN input_data = location and name like 'B%' THEN 1
     WHEN input_data = college and name like 'C%' THEN 1
   END = 1

添加您的条件,而不是name like 'B%'name like 'C%'条件。