在主查询中使用子查询



我做过这样的子查询但是它给出了错误

                    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

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

我认为您需要从datediff中删除e,使其成为sourcetable3.datdiff

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

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

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

如果你在Linux上,试着把它们都改成SourceTable3

看起来您在标识该列的行中出现了打字错误:

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

但是你应该把datediff括起来,因为它是一个保留的MySQL关键字。

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

那么,这个错误消息的哪个部分给您带来了麻烦?在sourcetable3中没有名为datediff的列,它是datdiff

相关内容

  • 没有找到相关文章

最新更新