在主查询中使用子查询

时间:2011-07-07 10:39:57

标签: mysql sql datetime mysql-error-1054

我像这样做了子查询,但它给出了错误

                    SELECT 
              sourceTable3.Total,
              (sourcetable3.datediff-1) as diff,
            ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(diff) MONTH), INTERVAL DAY(NOW()) -            memberToMship_DueDay DAY) As expdate,
              ExpPayments, 
            MonthlyAmount, 
            SourceTable3.ExpPayments2, 
             FROM
             (
             SELECT 
                SourceTable2.Total,
             (SourceTable2.datdiff-1) as diff,
             SourceTable2.datdiff,                 
            ExpPayments, 
            MonthlyAmount, 
            SourceTable2.ExpPayments2, 
           member_Id 
          FROM 
          (
             SELECT 
              SourceTable.Total,
               ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
              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
      ) as SourceTable2
  ) as sourceTable3

但它给出了这样的错误...

                         Error Code: 1054
                         Unknown column 'sourcetable3.datediff' in 'field list'

任何人都会帮忙......

修改后的代码:

修改了'e'并得到了这样的错误

错误代码:1054 '字段列表'中的未知列'memberTo_Mship_DueDay'

修改后的代码:

这是我的表结构

                                membership table
                              memberToMship_Id
                             memberToMship_StartDate
                             memberToMship_EndDate
                             memberToMship_DueDay
                             member_Id

                       I want finally these columns expdate , diff , ExpPayments, 
            MonthlyAmount, 





            MODIFIED CODE :




                SELECT 
           SourceTable3.Total,

          ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(diff) MONTH), INTERVAL DAY(NOW()) -          memberToMship_DueDay DAY) As expdate,
          (Sourcetable3.datdiff-1) as diff,
         ExpPayments, 
            MonthlyAmount, 
          SourceTable3.ExpPayments2, 
           SourceTable3.member_Id
          FROM
          (
              SELECT 
            SourceTable2.Total,
             (SourceTable2.datdiff-1) as diff,
            SourceTable2.datdiff,                 
            ExpPayments, 
            MonthlyAmount, 
           SourceTable2.ExpPayments2, 
            member_Id 
                FROM 
                (
                        SELECT 
               SourceTable.Total,
                 ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
                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
        ) as SourceTable2
      ) as SourceTable3

和错误

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

任何人都会帮忙......

5 个答案:

答案 0 :(得分:2)

我认为您需要从e移除datediff,使其变为sourcetable3.datdiff

答案 1 :(得分:1)

您对SourceTable3使用了不同的案例:

sourceTable3.Total,
(sourcetable3.datediff-1) as diff,
SourceTable3.ExpPayments2,
) as sourceTable3

如果您使用的是Linux,请尝试将它们全部更改为SourceTable3。请参阅Identifier Case Sensitivity

答案 2 :(得分:1)

SELECT 
      SourceTable2.Total,
      ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(diff) MONTH), INTERVAL DAY(NOW()) -memberToMship_DueDay DAY) As expdate,
      datdiff, 
      diff,
      SourceTable2.ExpPayments, 
      SourceTable2.MonthlyAmount, 
      (SourceTable2.Total/SourceTable2.MonthlyAmount) as ExpPayments2, 
      SourceTable2.expdate,
      SourceTable2.member_Id 
FROM
(    
    SELECT 
      memberToMship_DueDay,
      SourceTable.Total,
      SourceTable.expdate
      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.expdate,
      SourceTable.member_Id 
    FROM 
    ( 
        SELECT 
          memberToMship_DueDay,
          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
) as SourceTable2

答案 3 :(得分:0)

看起来您在识别该列的行中有拼写错误:

ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff, 
                                          -----------^^-----

但是你应该将datediff括在反引号中,因为它是一个保留的MySQL关键字。

ceil(SourceTable.ExpPayments-SourceTable.Total) AS `datediff`,

答案 4 :(得分:0)

嗯,这个错误信息的哪一部分给你带来麻烦? sourcetable3中根本没有名为datediff的列,它是datdiff