[英]MySQL Select when case
i have those tables and i want to check the same attribute at the same time: 我有那些表,我想同时检查相同的属性:
Person
id---name
1----Paul
2----Tom
3----Jim
Age
id---wert------personId
1----28--------1
2----25--------1
3----30--------3
i want to do something like this. 我想做这样的事情。
select * from Person p, Age where personId = p.id and CASE WHEN
name = 'Paul' THEN Age > 28 WHEN name = 'Tom' THEN Age <....
How it is possible? 怎么可能? With a CASE THEN in the WHERE clause?
在WHERE子句中使用CASE THEN? Please don't think about the structure of the table but only about the principle.
请不要考虑表格的结构,而只考虑原则。
Any Ideas? 有任何想法吗? Thank
谢谢
What you're trying to do is possible like so 您正在尝试做的事情可能像这样
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 I believe need to be part of an expression, not an expression themselves. 在这种情况下,我认为需要成为表达的一部分,而不是表达本身。 It's common use case is in the select, however you can use it in the where.
常见的用例是在select中,但是您可以在where中使用它。
In a select this would be: 选择一个是:
select
case name
when 'paul' then 28
when 'jim' then 30
end
from X
To use it within an where you would do this: 要在执行此操作的位置使用它:
select * from X where age = case name when 'jim' then 28 else 30 end
Depending on what you are attempting to achieve, you may want to consider OR statements instead of case in your where clause. 根据您要实现的目标,您可能需要考虑OR语句,而不是where子句中的大小写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.