2013-03-27 69 views
0

我遇到了一個mySQL查詢的問題。MySQL leftjoin和ORDER

我想返回每個credit_memoHISTORIQUE_DEVISE表中的最後匯率交換。

我對HISTORIQUE_DEVISE表中的記錄如下:

ID | account_id| code_source | code_destination | date  | taux  | .. 
242 |   12 |    1 |    133 | 2013-02-22 | 0.82000 | .. 
243 |   12 |    1 |    133 | 2013-03-26 | 0.96000 | .. 
244 |   12 |    1 |    133 | 2013-03-26 | 1.29000 | .. 

目前,我的最後一個左聯接返回3行(如果credit_memo.date等於2013-03-26並在SELECT返回0.82000rate.taux 哪有我。轉換查詢得到1.29000,我的左連接的最後一個值?因爲我只想要最後一條記錄#244。 是否有可能避免左連接內的子查詢?

謝謝你的幫助!

SELECT credit_memo.code_avoir_client, 
     credit_memo.total_ttc, 
     credit_memo.date, 
     credit_memo.code_utilisateur, 
     credit_memo.montant_restant_a_payer, 
     DEVISE.code_iso, 
     PAIEMENT_FACTURE_FOURNISSEUR.date_reglement, 
     customer.code_client, 
     customer.code_utilisateur_client, 
     customer.prenom, 
     customer.raison_social, 
     lead.code_client, 
     lead.code_utilisateur_client, 
     lead.prenom, 
     lead.raison_social, 
     project.project_code, 
     project.name, 
     rate.taux 
FROM AVOIR_CLIENT AS credit_memo 
     LEFT JOIN CLIENT AS customer 
       ON customer.code_client = credit_memo.code_client 
       AND customer.code_profil_client = 1 
       AND (customer.account_id = 0 
         OR customer.account_id = 12) 
       AND customer.etat = 0 
     LEFT JOIN CLIENT AS lead 
       ON lead.code_client = credit_memo.code_client 
       AND lead.code_profil_client = 2 
       AND (lead.account_id = 0 
         OR lead.account_id = 12) 
       AND lead.etat = 0 
     LEFT JOIN PROJECT AS project 
       ON project.project_code = credit_memo.project_code 
       AND (project.account_id = 0 
         OR project.account_id = 12) 
       AND project.etat = 0 
     LEFT JOIN DEVISE 
       ON DEVISE.code_devise = credit_memo.code_devise 
       AND (DEVISE.account_id = 0 
         OR DEVISE.account_id = 12) 
       AND DEVISE.etat = 0 
     LEFT JOIN PAIEMENT_FACTURE_FOURNISSEUR 
       ON PAIEMENT_FACTURE_FOURNISSEUR.code_avoir_client = 
       credit_memo.code_avoir_client 
       AND (PAIEMENT_FACTURE_FOURNISSEUR.account_id = 0 
         OR PAIEMENT_FACTURE_FOURNISSEUR.account_id = 12) 
       AND PAIEMENT_FACTURE_FOURNISSEUR.etat = 0 
     LEFT JOIN ETABLISSEMENT AS eta 
       ON eta.code_etablissement = 
       credit_memo.code_etablissement 
       AND (eta.account_id = 0 
         OR eta.account_id = 12) 
     LEFT JOIN HISTORIQUE_DEVISE AS rate 
       ON rate.code_etablissement = 
       credit_memo.code_etablissement 
       AND rate.CODE_DEVISE_SOURCE = credit_memo.CODE_DEVISE 
       AND rate.CODE_DEVISE_DESTINATION = eta.CODE_DEVISE 
       AND rate.date <= credit_memo.date 
       AND (rate.account_id = 0 
         OR rate.account_id = 12) 
       AND rate.etat = 0 
WHERE (credit_memo.account_id = 0 
      OR credit_memo.account_id = 12) 
     AND credit_memo.etat = 0 
GROUP BY credit_memo.code_avoir_client ; 

回答

0

您可以通過添加將其過濾掉的where子句來消除其他行。你的SQL是一個有點複雜,我沒有更多的知識理解,但在一般的where子句會是這樣的:

AND NOT EXISTS 
(Select * 
From HISTORIQUE_DEVISE rate_2 
WHERE rate.accoun_id=rate_2.accountid 
    ... add other keys fields here if they need to be equal ... 
    AND rate_2.ID > rate.ID 
) 

換句話說,離開了記錄,你找到一個「匹配的結果「在HISTORIQUE_DEVISE,但更高的ID