简体   繁体   中英

Max ID join sql

I have three sql tables,

1) Commandes, 2) Ligne_commande, 3) Produits.

commandes is linked with ligne_commande and ligne_commande is linked to produits.

Commandes : CMD_ID
Produits : PRD_ID
Ligne Commande : LGCM_CMD_ID, LGCM_PRD_ID

CMD_ID = LGCM_CMD_ID
LGCM_PRD_ID = PRD_ID

i'm getting this result :

(29) - 028001 - AC 1ERES C.DE BLAYE. CH L.BERTRANDS     1   

(30) - 028001 - AC 1ERES C.DE BLAYE. CH L.BERTRANDS     7   

(31) - 028001 - AC 1ERES C.DE BLAYE. CH L.BERTRANDS     7

I want to show only the last LGCM_ID , i tried to use MAX (LGCM_ID) but when i used it, i got (31) - 028001 - AC 1ERES C.DE BLAYE. CH L.BERTRANDS (31) - 028001 - AC 1ERES C.DE BLAYE. CH L.BERTRANDS but the value 1 not 7 for the quantity ordered... (LGCM_QTE_COMMANDE)

I need that because i want to show the products that the consummer has already ordered before.

<?php

foreach ($query=$db->query("SELECT
t1.CMD_ID, CMD_DATE, t1.CMD_CLT_ID,
MAX(t2.LGCM_ID) AS LGCM_LAST_ID, t2.LGCM_CMD_ID, t2.LGCM_PRD_ID, t2.LGCM_QTE_COMMANDE,
t3.PRD_ID, t3.PRD_CODE, t3.PRD_LIBELLE
FROM commandes t1
INNER JOIN ligne_commande t2
ON t1.CMD_ID = t2.LGCM_CMD_ID
LEFT OUTER JOIN produits t3
ON t2.LGCM_PRD_ID = t3.PRD_ID WHERE t1.CMD_CLT_ID = 418") AS $donnees):

echo '<tr>';
echo '<td>('.$donnees['LGCM_LAST_ID'].') - '.$donnees['PRD_CODE'].' - '.$donnees['PRD_LIBELLE'].'</td>';
echo '<td>/</td>';
echo '<td>'.$donnees['LGCM_QTE_COMMANDE'].'</td>';
echo '<td>/</td>';
echo '</tr>';

endforeach;

?>

Thanks.

EDIT : Look this picture :

http://nsa34.casimages.com/img/2014/01/03//140103055118239421.jpg

I want to show only the number 38, 37, 36. It's the last product that the consummer ordered.

Just ORDER BY your LGCM_ID DESC and limit your results to 1 record, which in mysql is LIMIT 1

Since I don't have your original query that returns 3 records, I can only give you the above recommendation. But use that query with those two additions that I specified and you should be good to go.

EDIT: The question has been updated. So what you really want to do is get the last order id for each product ordered by a particular customer.

To do that, see this JS Fiddle Example

SELECT cp.*, p.Name
FROM Products p
INNER JOIN Customer_Products cp ON cp.P_ID = p.ID
INNER JOIN 
  (SELECT MAX(cp.ID) AS Max_ID
  FROM Customer_Products cp
  WHERE cp.C_ID = 1
  GROUP BY cp.P_ID)
AS t0 ON t0.Max_ID = cp.ID;

The t0 INNER JOIN gets you the maximum ID of the order for each product ordered by a customer, which is achieved through the GROUP BY cp.P_ID statement. Then, given that max id, we join back to the Customer_Products to get details from that order, such as the QTY ordered. And working up one more level, we join to Products so we can get the product name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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