3

下午好,想知道在我有点挣扎的时候是否有人能指出我正确的方向。我有一个 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。

4

3 回答 3

3

这不被允许。当别名与其他列处于同一级别时,不能将该列用作别名SELECT

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

SELECT alias
FROM (SELECT column1 AS alias
      FROM table);
于 2012-05-23T08:17:53.987 回答
3

您可以在 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
于 2015-10-29T12:09:00.563 回答
-1

采用(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
于 2016-10-26T01:31:37.163 回答