2012-05-22 64 views
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 

这将返回一个错误“未知列‘付费’的字段列表”,有什么办法,我可以使用付费它被创建后的别名?我试图TP推出其在Access & SQL创建了一个新的系统,他们只需使用已保存的查询/ SP可获得的..

+0

灿你帮我查询? – JHS

+0

的MS-访问别名功能是非标准将无法正常工作/编译任何其他DBMS平台。您可以创建使用定义的函数来完成相同的事情。 –

+1

不能在MySQL的SELECT语句中使用别名,你必须重新计算 – Nico

回答

3

它不允许的。你不能使用列的别名时,别名和另一列是在SELECT同一水平。

您也可以使用别名,如果是这样的 -

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

您的输入好,谢谢,我设法通过使用MySQL视图进行排序此,作品一种享受... – gary

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 
+0

这应该是公认的答案。 – Ultimater

-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