2011-07-07 144 views
0

我做喜歡這樣子查詢,但它給錯誤在主查詢中使用子查詢

    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' 和有這樣

012錯誤

錯誤代碼: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' 

任何人會幫助...

+0

拜託,請檢查您的表 – wonk0

+0

@ wonk0我有一個像這些領域中memberTomship表中的列memberTo_Mship_DueDay ... memberToMship_EndDate,memberToMship_StartDate – user682417

+0

@ user682417你仍然有問題,請給你的桌面截圖與你想要的結果。 – rahularyansharma

回答

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

錯誤代碼:1054 未知列'差異'在'字段列表'這是我得到的錯誤... – user682417

+0

請參閱上面的編輯... – StevieG

+0

thanq stevie G現在工作... – user682417

2

我認爲你需要從datediff中刪除e,所以它變成sourcetable3.datdiff

+0

雅我已經刪除,但它給這樣的錯誤...錯誤代碼:1054。'字段列表'中的未知列'memberToMship_DueDay' – user682417

+0

@ user682417:我會檢查您所指的實際表格的列拼寫。 –

+0

問題就在這裏sourceTable3.Total, (sourcetable3.datdiff-1)as diff, ADDDATE(ADDDATE(NOW(),INTERVAL FLOOR(diff)MONTH),INTERVAL DAY(NOW()) - memberToMship_DueDay DAY) ,memberToMship_DueDay不在字段列表 – user682417

1

使用了不同的情況下,爲SourceTable3

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

如果你是在Linux上,嘗試所有更改爲SourceTable3。請參閱Identifier Case Sensitivity

0

看起來你有行一個錯字,其中該列中標識:

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

但是,你應該附上datediff在反引號,因爲它是一個保留MySQL的關鍵字。

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

多數民衆贊成在罰款..但現在我得到了這樣的不同錯誤...錯誤代碼:1054 未知列'memberToMship_DueDay'在'字段列表' – user682417

0

那麼,這個錯誤信息的哪一部分給你帶來麻煩? 是根本不存在的sourcetable3命名DATEDIFF列,它是datdiff

+0

錯誤代碼:1054未知列'memberTo_Mship_DueDay 'in'字段列表這是錯誤 – user682417

+0

@ user682417檢查針對實際表格的拼寫。同時檢查你是否使用相同的情況。如果你在linux上,列名和表名是區分大小寫的。 –

+0

我不使用linux ..我使用c#php和mysql ... – user682417