我想编写一个根据条件在运行时构造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>
我该怎么做呢?有可能吗?
答案 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%'
条件。