使用case语句mysql的变量

时间:2016-11-15 19:27:35

标签: mysql

我有这个查询

select 
count(so.shop_order_id),
case
    when so.subtotal_amount < 99.99 then 'under 100'
    when so.subtotal_amount between 100.00 and 149.99 then '100-149' 
    when so.subtotal_amount between 150.00 and 249.99 then '150-249'
    when so.subtotal_amount between 250.00 and 399.99 then '250-399' 
    when so.subtotal_amount between 400.00 and 499.99 then '400-499'
    when so.subtotal_amount > 500 then '500+'
end

from shop_order so

where so.created_at between '2015-11-29 23:59:59' and '2015-11-31 00:00:01'
and  (case
    when so.subtotal_amount < 99.99 then 'under 100'
    when so.subtotal_amount between 100.00 and 149.99 then '100-149' 
    when so.subtotal_amount between 150.00 and 249.99 then '150-249'
    when so.subtotal_amount between 250.00 and 399.99 then '250-399' 
    when so.subtotal_amount between 400.00 and 499.99 then '400-499'
    when so.subtotal_amount > 500 then '500+'
end) is not null

group by (case
    when so.subtotal_amount < 99.99 then 'under 100'
    when so.subtotal_amount between 100.00 and 149.99 then '100-149' 
    when so.subtotal_amount between 150.00 and 249.99 then '150-249'
    when so.subtotal_amount between 250.00 and 399.99 then '250-399' 
    when so.subtotal_amount between 400.00 and 499.99 then '400-499'
    when so.subtotal_amount > 500 then '500+'
end)

它适用于我,但我想知道是否有办法不必重复该案例陈述。我可以设置变量或使用别名或其他东西代替它吗?每次进行调整时,我都必须编辑case语句的每个实例。我对任何事情都持开放态度。谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用子查询:

select count(so.shop_order_id), so.thecase
from (
  select
    *,
    case
      when subtotal_amount < 99.99 then 'under 100'
      when subtotal_amount between 100.00 and 149.99 then '100-149' 
      when subtotal_amount between 150.00 and 249.99 then '150-249'
      when subtotal_amount between 250.00 and 399.99 then '250-399' 
      when subtotal_amount between 400.00 and 499.99 then '400-499'
      when subtotal_amount > 500 then '500+'
    end AS thecase
  from shop_order
) so
where
  so.created_at between '2015-11-29 23:59:59' and '2015-11-31 00:00:01'
  and so.thecase is not null
group by
  so.thecase

答案 1 :(得分:1)

这是@Uueerdo在评论中写的内容的实现。

在SELECT子句中使用表达式的别名(as range)。 GROUP BY使用该别名group by range。如果so.subtotal_amount IS NULL,表达式只能为NULL,因此您可以在WHERE子句中使用此条件。

您的表达也可以缩短。如果第一个条件(subtotal_amount < 100)失败,则表明subtotal_amount>= 100,因此您不需要BETWEEN语句。请注意,BETWEEN表示&#34;包括&#34;在MySQL中。 x BETWEEN a and bx >= a and x <= b相同。

另外 - 当你&#34;说&#34; &#39;低于100&#39;您应该使用< 100代替< 99.99。同样适用于&#39; 500 +&#39; - 它应该是>= 500。所以你的范围没有任何差距。

select 
    count(so.shop_order_id),
    case
        when so.subtotal_amount <  100 then 'under 100'
        when so.subtotal_amount <  150 then '100-149' 
        when so.subtotal_amount <  250 then '150-249'
        when so.subtotal_amount <  400 then '250-399' 
        when so.subtotal_amount <  500 then '400-499'
        when so.subtotal_amount >= 500 then '500+'
    end as range
from shop_order so
where so.created_at between '2015-11-29 23:59:59' and '2015-11-31 00:00:01'
  and so.subtotal_amount is not null
group by range