MySQL CASE 基于以前的 CASE 值

Leo*_*los 1 mysql view case subquery temp-tables

在 MySQL 中,是否可以CASESELECT子句中有两个语句,其中第二个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)

到目前为止我发现的唯一解决方案是两个生成一个临时表、视图或子查询,然后messagestatus这个子查询中返回的确定。

有没有办法在不使用临时表、视图或子查询的情况下编写此查询?如果可能的话,我试图避免这些构造以保持查询简单和优化。谢谢!

Bar*_*nka 9

您可以使用临时变量:

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)

关于临时变量你必须知道的一些事情:

  1. 他们总是在前面 @
    • 避免使用保留字,以防万一(这就是我命名变量的原因 @status1
  2. 在之后@的象征,他们必须以字母开头,并且一定不能有空格
  3. 当您在单个查询中更新它们时,它们会“从左到右”(谈论列)和“从头到尾”(谈论行)更新。这可以帮助您计算累积总和或平均值。

示例(对于第 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)

临时变量可以帮助你做一些很棒的事情......随意玩弄;)


更新

如果要在此查询的结果上定义条件,有两种方法:

  1. 使用上述查询作为第二个查询的数据源(即使其成为from另一个查询子句中的子查询
  2. 创建一个临时表并对其进行查询

选项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)

关于临时表你必须知道的事情:

  • 它们是在 RAM 上创建的(默认情况下,并且仅当表不是太大时)
  • 它们仅对创建它的连接可见
  • 一旦创建它的连接关闭(或以任何方式终止),它们就会被消除
  • 您不能在一个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,并且不是指临时变量,而是指列的别名。