简体   繁体   English

MySQL leftjoin和ORDER

[英]MySQL leftjoin and ORDER

I am encountering an issue with a mySQL query. 我在使用mySQL查询时遇到问题。

I want to return for each credit_memo the last rate exchange from the HISTORIQUE_DEVISE table. 我想为每个credit_memo返回HISTORIQUE_DEVISE表中的最后一次汇率交换。

My records for the HISTORIQUE_DEVISE table are the following : 我在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 | ..

Currently, my last left join returns the 3 rows (if the credit_memo.date equals 2013-03-26 and the rate.taux in the SELECT returns 0.82000 . How can I transform the query to obtain 1.29000 , the last value of my left join? because I only want the last record #244. Is it possible to avoid subquery inside the left join? 目前,我的最后一个左credit_memo.date返回3行(如果credit_memo.date等于2013-03-26 ,而SELECT中的rate.taux返回0.82000 。如何转换查询以获得1.29000 ,这是我的左1.29000的最后一个值?因为我只想要最后一条记录#244。是否可以避免左联接内的子查询?

Thank you for your help guys! 谢谢您的帮助!

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 ;

You can eliminate the other rows by adding a where clause that filter them out. 您可以通过添加将其过滤掉的where子句来消除其他行。 Your SQL is a little complicated for me to understand without more knowledge, but in general the where clause would be something like this: 在没有更多知识的情况下,您的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
)

In other words, leave out records where you find a "matching record(s)" in HISTORIQUE_DEVISE, but with a higher ID 换句话说,请删除在HISTORIQUE_DEVISE中找到“匹配记录”的记录,但ID较高

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM