MySQL Select 当大小写
MySQL Select when case
我有那些 table,我想同时检查相同的属性:
Person
id---name
1----Paul
2----Tom
3----Jim
Age
id---wert------personId
1----28--------1
2----25--------1
3----30--------3
我想做这样的事情。
select * from Person p, Age where personId = p.id and CASE WHEN
name = 'Paul' THEN Age > 28 WHEN name = 'Tom' THEN Age <....
怎么可能?在 WHERE 子句中使用 CASE THEN?请不要考虑table的结构,只考虑原理。
有什么想法吗?
感谢
你想做的事情是可能的
select
*
from
Person p, Age
where personId = p.id
and CASE
WHEN name = 'Paul' THEN Age > 28
WHEN name = 'Tom' THEN Age <....
WHEN expr then expr that evals to bool
END
CASE WHEN THEN 我认为需要成为表达式的一部分,而不是表达式本身。它的常见用例是在 select 中,但是您可以在 where.
中使用它
在 select 这将是:
select
case name
when 'paul' then 28
when 'jim' then 30
end
from X
要在需要执行此操作的地方使用它:
select * from X where age = case name when 'jim' then 28 else 30 end
根据您要实现的目标,您可能需要考虑在 where 子句中使用 OR 语句而不是大小写。
我有那些 table,我想同时检查相同的属性:
Person
id---name
1----Paul
2----Tom
3----Jim
Age
id---wert------personId
1----28--------1
2----25--------1
3----30--------3
我想做这样的事情。
select * from Person p, Age where personId = p.id and CASE WHEN
name = 'Paul' THEN Age > 28 WHEN name = 'Tom' THEN Age <....
怎么可能?在 WHERE 子句中使用 CASE THEN?请不要考虑table的结构,只考虑原理。
有什么想法吗? 感谢
你想做的事情是可能的
select
*
from
Person p, Age
where personId = p.id
and CASE
WHEN name = 'Paul' THEN Age > 28
WHEN name = 'Tom' THEN Age <....
WHEN expr then expr that evals to bool
END
CASE WHEN THEN 我认为需要成为表达式的一部分,而不是表达式本身。它的常见用例是在 select 中,但是您可以在 where.
中使用它在 select 这将是:
select
case name
when 'paul' then 28
when 'jim' then 30
end
from X
要在需要执行此操作的地方使用它:
select * from X where age = case name when 'jim' then 28 else 30 end
根据您要实现的目标,您可能需要考虑在 where 子句中使用 OR 语句而不是大小写。