MySQL案例基于以前的案例值
在MySQL中,MySQL案例基于以前的案例值,mysql,view,subquery,case,temp-tables,Mysql,View,Subquery,Case,Temp Tables,在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` F
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`
到目前为止,我找到的唯一解决方案是生成一个临时表、视图或子查询,然后由该子查询中返回的状态决定消息
有没有一种不使用临时表、视图或子查询的方法来编写此查询?如果可能的话,我试图避免这些构造,以保持查询的简单和优化。谢谢大家! 您可以使用临时变量:
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;
示例(针对第3点):
select @t := 1, @t := @t + 1;
@t1 | @t2
----+----
1 | 2
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
select a.*
from (
-- The query with temp variables defined
)
where -- ATTENTION: you need to write the references to the column names of the subquery
临时变量可以帮助你做一些很棒的事情。。。随便玩玩吧;)
更新
如果要定义此查询结果的条件,有两种方法:
将上述查询用作第二个查询的数据源(即,将其作为另一个查询的from
子句中的子查询)
创建临时表并对其进行查询
选项1:
select @t := 1, @t := @t + 1;
@t1 | @t2
----+----
1 | 2
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
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:(我个人的最爱)
不要使用where
使用have
,不要引用temp变量,而是引用列的别名。可以使用临时变量:
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;
示例(针对第3点):
select @t := 1, @t := @t + 1;
@t1 | @t2
----+----
1 | 2
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
select a.*
from (
-- The query with temp variables defined
)
where -- ATTENTION: you need to write the references to the column names of the subquery
临时变量可以帮助您做一些很棒的事情……请随意玩;)
更新
如果要定义此查询结果的条件,有两种方法:
将上述查询用作第二个查询的数据源(即,将其作为另一个查询的from
子句中的子查询)
创建临时表并对其进行查询
选项1:
select @t := 1, @t := @t + 1;
@t1 | @t2
----+----
1 | 2
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
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:(我个人的最爱)
不要使用where
使用having
,不要引用temp变量,而是引用列的别名。我认为如果没有至少一个子查询,您就无法做到这一点。而且我认为这不会导致任何性能问题:)@RodrigoAssis它可以做到(请检查下面的答案;))很好!学习每一个day@RodrigoAssis这正是我在了解它们时所说的(就在这里的SO中),我认为如果没有至少一个子查询,就无法做到这一点。我认为这不会导致任何性能问题:)@Rodrigassis它可以做到(检查下面的答案;))很好!学习每一个day@RodrigoAssis这正是我在了解他们时所说的话(就在这里),谢谢你的回答。看起来我不能在WHERE
子句中使用临时变量,对吗?例如,如果我在查询的末尾添加WHERE@status1='awesome'
,则结果为零。@Leo事实上,临时变量不能在WHERE子句中使用。然而,如果你需要的话,还有一个解决办法。。。更新以包含“技巧”好的,非常感谢您的更新。看起来,如果我想使用多个CASE
语句并对临时变量进行筛选,我将不得不使用临时表或子查询,毕竟:|@Leo Yes。。。但是您可以使用temp变量进行所有复杂的替换(或计算),然后使用结果执行更简单的查询。没有免费的午餐,但您可以通过一些计算将一个复杂的过程拆分为几个简单的步骤,最终的查询将非常简单。“分而治之”在90%的情况下有效(至少如此),而且通常比试图在一个过于复杂的环境中做事情要快query@Leo检查新的更新。。。还有第三件事你想做的谢谢你的回答。看起来我不能在WHERE
子句中使用临时变量,对吗?例如,如果我在查询的末尾添加WHERE@status1='awesome'
,则结果为零。@Leo事实上,临时变量不能在WHERE子句中使用。然而,如果你需要的话,还有一个解决办法。。。更新以包含“技巧”好的,非常感谢您的更新。看起来,如果我想使用多个CASE
语句并对临时变量进行筛选,我将不得不使用临时表或子查询,毕竟:|@Leo Yes。。。但是您可以使用temp变量进行所有复杂的替换(或计算),然后使用结果执行更简单的查询。没有免费的午餐,但您可以通过一些计算将一个复杂的过程拆分为几个简单的步骤,最终的查询将非常简单。“分而治之”在90%的情况下有效(至少如此),而且通常比试图在一个过于复杂的环境中做事情要快query@Leo检查新的更新。。。你想干什么就干什么