【问题标题】:Join Alias and Where in SQL在 SQL 中加入别名和位置
【发布时间】:2014-09-26 15:13:55
【问题描述】:

我正在尝试运行一个可以在 where 子句中使用别名的查询

例如:

Select V.Fees - Coalesce(V.Payment,0) - Coalesce (V.Adjustment,0) AS Balance
[
the inner join query for the tables
]
Where A.Order not Null and Balance = 0
Order by Name

但是像往常一样在哪里无法识别Balance?

我们如何解决这个问题?

【问题讨论】:

  • 使用子查询或重复表达式或在 MySQL 中使用 having 子句。
  • 或者只是在 where 中重复操作:Where A.Order not Null and V.Fees - Coalesce(V.Payment,0) - Coalesce (V.Adjustment,0) = 0
  • 拥有可能是最好的选择:stackoverflow.com/questions/2905292/where-vs-having

标签: sql join where alias


【解决方案1】:

执行您要求的唯一方法是将该字段放在 CTE 中并通过 CTE 的连接引用它。

【讨论】:

    【解决方案2】:

    你可以在where子句中重复表达式

    Select V.Fees - Coalesce(V.Payment,0) - Coalesce (V.Adjustment,0) AS Balance
    [
    the inner join query for the tables
    ]
    Where A.Order not Null and (V.Fees - Coalesce(V.Payment,0) - Coalesce (V.Adjustment,0)) = 0
    Order by Name
    

    【讨论】:

    • 非常感谢它的帮助。
    • @soumyasubhraKonar,如果它适合你,你可以接受答案
    猜你喜欢
    • 2017-04-02
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多