我有一个VARCHAR,值aaa或bbb说 依赖于此我必须做出
SELECT x FROM y WHERE y AND som = 1
或
SELECT x FROM y WHERE y AND som = 2
我有什么办法让这个像
SELECT i AS j,
case xxx
when z then som1
else som2
end as ABC
FROM ...
// EDIT
我想制作这样的东西(比如说它的伪代码;))
variable VARCHAR;
SELECT id FROM table WHERE age =
CASE variable
WHEN 'aaa' THEN 21
ELSE 'bbb' THEN 99
END
答案 0 :(得分:5)
您可以使用CASE
声明
SELECT id
FROM table
WHERE age = (CASE WHEN variable = 'aaa'
THEN 21
WHEN variable = 'bbb'
THEN 99
ELSE null
END)
但是,只需执行OR
SELECT id
FROM table
WHERE (variable = 'aaa' AND age = 21)
OR (variable = 'bbb' AND age = 99)