MySQL CASE基于以前的CASE值

时间:2014-07-03 21:22:56

标签: mysql view subquery case 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`

基本上,用户ID确定状态,然后状态确定消息。

但是,正如您可能已经猜到的,此查询会生成此错误:

Unknown column 'status'

到目前为止我找到的唯一解决方案是生成一个临时表,视图或子查询,然后message由此子查询中返回的status确定。

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

1 个答案:

答案 0 :(得分:4)

您可以使用临时变量:

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;

关于临时变量你必须了解的一些事情:

  1. 始终以@开头
    • 避免使用保留字,以防万一(这就是我将变量命名为@status1
    • 的原因
  2. @符号后,它们必须以字母开头,且不得有空格
  3. 当您在单个查询中更新它们时,它们会更新&#34;从左到右&#34; (谈论专栏)和#34;从头到尾&#34; (谈论行)。这可以帮助您计算累计金额或平均值。
  4. 示例(针对第2点):

    select @t := 1, @t := @t + 1;
    
    @t1 | @t2
    ----+----
    1   | 2
    

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

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


    <强> 更新

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

    1. 使用上述查询作为第二个查询的数据源(即将其作为另一个查询的from子句中的子查询
    2. 创建临时表并在其上查询
    3. 选项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
      

      选项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 ...
      ;
      

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

      • 它们是在RAM上创建的(默认情况下,仅当表格不是太大时)
      • 它们仅对创建它的连接可见
      • 一旦创建它的连接关闭(或以任何方式终止),它们就会被删除
      • 您无法在FROM子句中多次使用它。除此之外,您可以将其用作数据库中的任何其他表

      另一次更新

      我偶然遇到this question and its answer。如果您想使用列的结果(使用临时变量计算)作为条件,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';
      

      而不是使用where使用having,而不是引用temp变量,而是引用列的别名。

相关问题