使用查询结果进一步计算这些结果在同一查询中给出错误

时间:2011-07-07 08:53:47

标签: mysql sql error-code

我写了这样的查询  我想进一步计算这些结果,以单一查询

SELECT  TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
   DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments,
   30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount, 
   (totalcontractamountperiod/monthlyamount) as exppayments, 
   member_Id 
FROM membertomships;

是否有可能全部使用mysql进行单一查询

但它给出了这样的错误

Error Code: 1054
Unknown column 'totalcontractamountperiod' in 'field list'

任何人都可以了解这个

修改后的代码:我这样做是通过使用子查询....

SELECT  
  EXPPAYMENT WHERE EXPPAYMENT =(TIMESTAMPDIFF(MONTH, memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments , 
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount , 
  (totalcontractamountperiod/monthlyamount) as exppayments, member_Id 
FROM membertomships);

但它给出了这样的错误

Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE EXPPAYMENT =(TIMESTAMPDIFF(MONTH, memberToMship_StartDate,memberToMship_En' at line 1

这是修改后的查询,并为查询添加了两个函数,但是它给出了错误

SELECT 
  Total,
  (datdiff-1) as diff,
  ceil(ExpPayments-Total) AS datdiff,                 
  ExpPayments, 
  MonthlyAmount, 
  (Total/MonthlyAmount) as ExpPayments2, 
  member_Id 
FROM 
( 
  SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*    memberToMship_ChargePerPeriod As Total,     
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
  member_Id
FROM 
  membertomships 
) as SourceTable;

像这样的错误

                     Error Code: 1054
                     Unknown column 'datdiff' in 'field list'
你会帮忙吗

               SELECT 
                SourceTable.Total,
               ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
              ceil(SourceTable.ExpPayments-SourceTable.Total) -1 as diff,
                       ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(ceil(SourceTable.ExpPayments-   SourceTable.Total) -1) MONTH), INTERVAL DAY(NOW()) - SourceTable.memberToMship_DueDay DAY) As expdate,
              SourceTable.ExpPayments, 
             SourceTable.MonthlyAmount, 
           (SourceTable.Total/SourceTable.MonthlyAmount) as ExpPayments2, 
            SourceTable.member_Id 
        FROM 
        ( 
              SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*      memberToMship_ChargePerPeriod As Total,     
          DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
          30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
           member_Id
          FROM 
         membertomships 
      ) as SourceTable

错误代码:1054 '字段列表'中的未知列'SourceTable.memberToMship_DueDay'

2 个答案:

答案 0 :(得分:1)

SELECT 
  derived.totalcontractamountperiod,
  derived.exppayments,
  derived.monthlyamount,
  (derived.totalcontractamountperiod/derived.monthlyamount) as exppayments2,
  derived.member_Id
FROM
( 
  SELECT  
    TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
    DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments,
    30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount, 
    (totalcontractamountperiod/monthlyamount) as exppayments, 
    member_Id 
  FROM membertomships
) derived

修改后的代码遇到了同样的问题。它应该是:

SELECT 
  SourceTable.Total,
  ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
  ceil(SourceTable.ExpPayments-SourceTable.Total) -1 as diff,
  SourceTable.ExpPayments, 
  SourceTable.MonthlyAmount, 
  (SourceTable.Total/SourceTable.MonthlyAmount) as ExpPayments2, 
  SourceTable.member_Id 
FROM 
( 
  SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*      memberToMship_ChargePerPeriod As Total,     
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
  member_Id
  FROM 
    membertomships 
) as SourceTable

答案 1 :(得分:1)

我会删除一些列名,因为它们有点长,所以查询可以更清楚:

SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
        DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
        30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
--->    (Total/MonthlyAmount) as ExpPayments2,     
        member_Id  
FROM membertomships; 

问题在于指向的行,因为您在定义它们的同一列列表中使用TotalMonthlyAmount。我知道它看起来应该可以工作,但是像这样使用SQL是违法的。

你能做的是:

  1. 重复定义值的条件(并希望数据库只计算一次),如
  2. 这样:

    SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
            DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
            30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
            (TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod / (30* ChargePerPeriod / DATEDIFF(EndDate,StartDate))) as ExpPayments2,     
            member_Id  
    FROM membertomships; 
    
    1. 像子查询一样重写表达式。您可以一步完成计算,然后将这些结果用于进一步计算,例如
    2. 这样:

      SELECT Total, ExpPayments, MonthlyAmount, (Total/MonthlyAmount) as ExpPayments2, member_Id  from
      (
        SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
                DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
                30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
                member_Id  
        FROM membertomships
      ) as SourceTable
      

      BTW,原始查询中的两列名为ExpPayments,我重命名为第二个ExpPayments2。