【问题标题】:Reuse Alias on calculated field在计算字段上重用别名
【发布时间】:2012-05-22 15:35:39
【问题描述】:

下午好,不知道在我有点挣扎时是否有人能指出我正确的方向。我有一个 mysql 查询,我需要在计算字段中包含一个别名:

 Select tblComms._CommMonth, 
        tblComms._Reference, 
        tblComms._ClientName, 
        tblComms._Premium, 
        tblComms._CommDue, 
        tblComms._Employee_Name, 
        tblCont.Retention, 
        (tblComms._CommDue) * (tblCont.Retention) / 100 As Paid, 
        (tblComms._CommDue) - (Paid) As Payable 
 From tblComms Inner Join dbo_companyref On 
      dbo_companyref._Reference = tblComms._Reference 
      Inner Join tblCont 
      On dbo_companyref._Advisor_Name = tblCont._Employee_Name

这会返回错误“字段列表中的未知列'付费'”,有什么方法可以在创建付费别名后使用它?我正在尝试推出一个在 Access 和 SQL 中创建的新系统,他们只是为此使用保存的查询/SP..

【问题讨论】:

  • 你能帮我查询一下吗?
  • MS-Access 别名功能是非标准的,不能为任何其他 DBMS 平台工作/编译。您可以创建用户定义的函数来完成同样的事情。
  • Mysql的select语句中不能使用别名,需要重新计算
  • @Juniad,我已经编辑了原始代码以反映实际的 sql 查询,抱歉我应该[最初发布完整代码。

标签: mysql subquery


【解决方案1】:

这是不允许的。当别名和其他列在SELECT 的同一级别时,您不能将该列用作别名。

如果是这样的话,你可以使用别名 -

SELECT alias
FROM (SELECT column1 AS alias
      FROM table);

【讨论】:

  • 好的,感谢您的输入,我设法通过使用 mysql 视图对其进行了排序,很有效..
【解决方案2】:

你可以在 mysql 中为这些东西使用变量:

Select tblComms._CommMonth, 
    tblComms._Reference, 
    tblComms._ClientName, 
    tblComms._Premium, 
    tblComms._CommDue, 
    tblComms._Employee_Name, 
    tblCont.Retention, 
    @Paid := (tblComms._CommDue) * (tblCont.Retention) / 100 As Paid, 
    (tblComms._CommDue) - (@Paid) As Payable From tblComms Inner Join dbo_companyref On 
  dbo_companyref._Reference = tblComms._Reference 
  Inner Join tblCont 
  On dbo_companyref._Advisor_Name = tblCont._Employee_Name

【讨论】:

    【解决方案3】:

    使用(select Paid)

     Select tblComms._CommMonth, 
        tblComms._Reference, 
        tblComms._ClientName, 
        tblComms._Premium, 
        tblComms._CommDue, 
        tblComms._Employee_Name, 
        tblCont.Retention, 
        (tblComms._CommDue) * (tblCont.Retention) / 100 As Paid, 
        (tblComms._CommDue) - (select Paid) As Payable 
      From tblComms Inner Join dbo_companyref On 
      dbo_companyref._Reference = tblComms._Reference 
      Inner Join tblCont 
      On dbo_companyref._Advisor_Name = tblCont._Employee_Name
    

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多