2012-02-17 42 views
0

任何人都可以幫助解決這個問題嗎?我嘗試了所有我能想到的,沒有運氣的事情。如何修復MySQL查詢以解決「未知列......在子句中」錯誤?

我有以下MySQL查詢和它吐出以下錯誤

錯誤: 未知列在「SR.sales_rec_id '的條款'

QUERY:

SELECT 
    SYSKEY.cri_sr_id, 
    SR.sales_rec_id, 
    SYSKEY.adnum, 
    charge, 
    SYSKEY.cancelsale, 
    paymentdue, 
    merchant, 
    paymentnum, 
    authorization as authcode, 
    CONCAT(lastname,', ',firstname,' ',middleinitial) as customer, 
    CONCAT(replname,', ',repfname) as repname,  
FROM CRI_SR_KEYS as SYSKEY 
    INNER JOIN SALES_PENDING as SPEND on SPEND.sales_rec_id=SR.sales_rec_id 
    INNER JOIN SALES_RECORD as SR on SR.cri_sr_id=SYSKEY.cri_sr_id 
    LEFT JOIN CLIENT_INFO as CI on CI.client_id = SYSKEY.clientid 
    LEFT JOIN SALES_REPS as REP on SR.rep_id=REP.repid  
WHERE SPEND.authorization NOT LIKE '000000' AND paymentdue LIKE CURDATE() 
ORDER BY adnum ASC LIMIT 0,5000 
+0

注有repname'後'一個錯誤的逗號'FROM'子句之前... – 2012-02-17 03:52:27

+1

加入表** SALES_RECORD爲SR * *之前** SALES_PENDING作爲SPEND ** – 2012-02-17 05:11:17

回答

2

重新排列您的聯接的順序,以便在嘗試使用它之前SR是別名:

SELECT 
    SYSKEY.cri_sr_id,SR.sales_rec_id, 
    SYSKEY.adnum,charge, 
    SYSKEY.cancelsale,paymentdue,merchant,paymentnum,authorization as authcode, 
    CONCAT(lastname,', ',firstname,' ',middleinitial) as customer, 
    CONCAT(replname,', ',repfname) as repname  
FROM CRI_SR_KEYS as SYSKEY 
    INNER JOIN SALES_RECORD as SR on SR.cri_sr_id=SYSKEY.cri_sr_id 
    INNER JOIN SALES_PENDING as SPEND on SPEND.sales_rec_id=SR.sales_rec_id 
    LEFT JOIN CLIENT_INFO as CI on CI.client_id = SYSKEY.clientid 
    LEFT JOIN SALES_REPS as REP on SR.rep_id=REP.repid  
WHERE SPEND.authorization NOT LIKE '000000' AND paymentdue LIKE CURDATE() 
ORDER BY adnum ASC LIMIT 0,5000 

在SELECT子句中,repname之後還有一個逗號逗號,但這可能只是一個錯字。

+0

謝謝!是的,額外的逗號是一個錯字。這個問題只是通過重新安排連接來解決。 – Mattia 2012-02-17 03:52:39

1

如果列SALES_RECORD.sales_rec_id不實際存在的,重新排序JOIN聲明:

FROM CRI_SR_KEYS as SYSKEY 
    /* Move this above SALES_PENDING to SR */ 
    INNER JOIN SALES_RECORD as SR on SR.cri_sr_id=SYSKEY.cri_sr_id 
    INNER JOIN SALES_PENDING as SPEND on SPEND.sales_rec_id=SR.sales_rec_id 
    LEFT JOIN CLIENT_INFO as CI on CI.client_id = SYSKEY.clientid 
    LEFT JOIN SALES_REPS as REP on SR.rep_id=REP.repid  
+0

謝謝!我不敢相信這很簡單。完美工作。 – Mattia 2012-02-17 03:51:22

相關問題