TSQL - 在 Where 子句中使用派生的选择列 [英] TSQL - Use a Derived Select Column in the Where Clause

查看:42
本文介绍了TSQL - 在 Where 子句中使用派生的选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 TSQL 中有没有办法做这样的事情:

Is there a way in TSQL to do something like this:

select a,b,c,
case 
  when a=1 then 5
  when a=2 then 6
end as d
from some_table
where d=6

实际的 case 语句非常复杂,所以我试图避免在 where 子句中重复它?有什么技巧可以做到这一点吗?

The actual case statement is really complex, so I'm trying to avoid repeating it in the where clause? Are there any tricks to do this?

(我认为 MySQL 中有一个技巧可以使用使 d=6").

(I think there's a trick in MySQL to use "having d=6").

推荐答案

select
    a, b, c
from (
    select
        a, b, c,
        case 
          when a=1 then 5
          when a=2 then 6
        end as d
    from some_table
) as t
where d=6

这篇关于TSQL - 在 Where 子句中使用派生的选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆