【问题标题】:MySQL CASE based on previous CASE valueMySQL CASE 基于先前的 CASE 值
【发布时间】:2014-08-25 03:44:08
【问题描述】:

在 MySQL 中,SELECT 子句中是否可以有两个 CASE 语句,其中第二个 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 确定。

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

【问题讨论】:

  • 我认为如果没有至少一个子查询,您将无法做到这一点。而且我认为它不会导致任何性能问题:)
  • @RodrigoAssis 可以做到(检查下面的答案;))
  • 不错!每天学习
  • @RodrigoAssis 这正是我在了解它们时所说的(就在这里)

标签: mysql view subquery case temp-tables


【解决方案1】:

您可以使用临时变量:

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. 当您在单个查询中更新它们时,它们会“从左到右”(关于列)和“从前到最后”(关于行)进行更新。这可以帮助您计算累积和或平均值。

示例(针对第 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. 创建一个临时表并对其进行查询

选项 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,并且不是引用临时变量,而是引用列的别名。

【讨论】:

  • 感谢您的回答。看起来我不能在 WHERE 子句中使用临时变量,这是正确的吗?例如,如果我在查询末尾添加WHERE @status1 = 'awesome',我得到的结果为零。
  • @Leo 实际上,临时变量不能在 where 子句中使用。但是,如果您需要,有一种解决方法......更新以包含“技巧”
  • 好的,非常感谢您的更新。看起来如果我想使用多个 CASE 语句并过滤临时变量,我将不得不使用临时表或子查询:|
  • @Leo 是的...但是您可以使用临时变量进行所有复杂的替换(或计算),然后使用结果执行更简单的查询。没有免费的午餐,但您可以通过一些计算将复杂的过程拆分为几个简单的步骤,您的最终查询将是一个非常简单的查询。 “分而治之”(至少)在 90% 的情况下有效,并且通常比尝试在单个过于复杂的查询中做事要快
  • @Leo 检查新的更新...您想要的还有第三个解决方法
猜你喜欢
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 2019-02-15
相关资源
最近更新 更多