Leo*_*los 1 mysql view case subquery temp-tables
在 MySQL 中,是否可以CASE在SELECT子句中有两个语句,其中第二个CASE语句依赖于第一个CASE语句?
例如,考虑以下查询:
SELECT CASE WHEN `user`.`id` < 500 THEN 'awesome' ELSE 'lame' END
AS `status`
, CASE WHEN `status` = 'awesome' THEN 'You rock' ELSE 'You stink' END
AS `message`
FROM `user`
Run Code Online (Sandbox Code Playgroud)
基本上,用户 ID 决定状态,然后状态决定消息。
但是,正如您可能已经猜到的,此查询会生成以下错误:
Unknown column 'status'
Run Code Online (Sandbox Code Playgroud)
到目前为止我发现的唯一解决方案是两个生成一个临时表、视图或子查询,然后message由status这个子查询中返回的确定。
有没有办法在不使用临时表、视图或子查询的情况下编写此查询?如果可能的话,我试图避免这些构造以保持查询简单和优化。谢谢!
您可以使用临时变量:
select
@status1 := (case
when user.id < 500 then 'awesome'
else 'lame'
end) as `status`,
(case
when @status1 = 'awesome' then 'You rock'
else 'You stink'
end) as message
from
user;
Run Code Online (Sandbox Code Playgroud)
关于临时变量你必须知道的一些事情:
@
@status1@的象征,他们必须以字母开头,并且一定不能有空格示例(对于第 2 点):
select @t := 1, @t := @t + 1;
@t1 | @t2
----+----
1 | 2
Run Code Online (Sandbox Code Playgroud)
示例(针对第 3 点):
select myTable.x, @t := @t + myTable.x as cummulative_x
from
(select @t := 0) as init, -- You need to initialize the variable,
-- otherwise the results of the evaluation will be NULL
myTable
order by myTable.x -- Always specify how to order the rows,
-- or the cummulative values will be quite odd
-- (and maybe not what you want)
;
x | cummulative_x
---+---------------
1 | 1
1 | 2
2 | 4
3 | 7
Run Code Online (Sandbox Code Playgroud)
临时变量可以帮助你做一些很棒的事情......随意玩弄;)
更新
如果要在此查询的结果上定义条件,有两种方法:
from另一个查询子句中的子查询选项1:
select a.*
from (
-- The query with temp variables defined
)
where -- ATTENTION: you need to write the references to the column names of the subquery
Run Code Online (Sandbox Code Playgroud)
选项2:(我个人最喜欢的)
drop table if exists temp_my_temp_table;
create temporary table temp_my_temp_table
select
@status1 := (case
when user.id < 500 then 'awesome'
else 'lame'
end) as `status`,
(case
when @status1 = 'awesome' then 'You rock'
else 'You stink'
end) as message
from
user;
-- Add all appropriate indexes to this newly created table:
-- alter table temp_my_temp_table
-- add index idx_status(`status`),
-- add index idx_mess(message);
-- Make your queries on this new temp table
select * from temp_my_temp_table
-- where ...
;
Run Code Online (Sandbox Code Playgroud)
关于临时表你必须知道的事情:
FROM子句中多次使用它。除此之外,您可以将其用作数据库中的任何其他表另一个更新
我偶然遇到了这个问题及其答案。如果您想使用列的结果(使用临时变量计算)作为条件,MySQL 允许这样做:
select
@status1 := (case
when user.id < 500 then 'awesome'
else 'lame'
end) as `status`,
(case
when @status1 = 'awesome' then 'You rock'
else 'You stink'
end) as message
from
user
having
`status` = 'awesome';
Run Code Online (Sandbox Code Playgroud)
而不是使用whereuse having,并且不是指临时变量,而是指列的别名。