Jithin Varghese
Jithin Varghese

Reputation: 2228

how to get last row from table 2 using left join mysql

I have tow tables tbl_product_checkout and tbl_product_checkout_status in which I want to get the last row from tbl_product_checkout_status

//tbl_product_checkout

product_checkout_id    user_id    product_checkout_order_no
-----------------------------------------------------------
        1                 1              ORD123456


//tbl_product_checkout_status

checkout_status_id    product_checkout_id    checkout_status_check
------------------------------------------------------------------
        1                      1                  Dispatched
        2                      1                  Delivered

I have tried using the following query

SELECT * 
  FROM tbl_product_checkout pc 
  LEFT 
  JOIN tbl_product_checkout_status cs 
    ON cs.product_checkout_id = pc.product_checkout_id  
 WHERE pc.user_id = 1 
 GROUP 
    BY pc.product_checkout_id 
 ORDER 
    BY cs.checkout_status_id DESC

but the output for above query is,

user_id    product_checkout_order_no    checkout_status_check
-------------------------------------------------------------
   1              ORD123456                  Dispatched

but I want the result as,

user_id    product_checkout_order_no    checkout_status_check
-------------------------------------------------------------
   1              ORD123456                  Delivered

Upvotes: 0

Views: 48

Answers (2)

P.Salmon
P.Salmon

Reputation: 17665

Add a where = max sub query eg

DROP TABLE IF EXISTS tbl_product_checkout,tbl_product_checkout_status;

CREATE TABLE tbl_product_checkout(product_checkout_id INT,   user_id INT,   product_checkout_order_no VARCHAR(20));
INSERT INTO tbl_product_checkout VALUES
(        1       ,          1  ,            'ORD123456');


CREATE TABLE tbl_product_checkout_status(checkout_status_id INT,   product_checkout_id INT,    checkout_status_check VARCHAR(20));
INSERT INTO tbl_product_checkout_status VALUES
(        1      ,                1 ,                 'Dispatched'),
(        2      ,                1 ,                 'Delivered');

SELECT * FROM
tbl_product_checkout T1
LEFT JOIN tbl_product_checkout_status T2 ON T1.PRODUCT_CHECKOUT_ID = T2.PRODUCT_CHECKOUT_ID
WHERE T2.CHECKOUT_STATUS_ID = (
SELECT MAX(T3.CHECKOUT_STATUS_ID) 
FROM tbl_product_checkout_status T3 
WHERE T3.PRODUCT_CHECKOUT_ID = T2.PRODUCT_CHECKOUT_ID
)
;

Result

+---------------------+---------+---------------------------+--------------------+---------------------+-----------------------+
| product_checkout_id | user_id | product_checkout_order_no | checkout_status_id | product_checkout_id | checkout_status_check |
+---------------------+---------+---------------------------+--------------------+---------------------+-----------------------+
|                   1 |       1 | ORD123456                 |                  2 |                   1 | Delivered             |
+---------------------+---------+---------------------------+--------------------+---------------------+-----------------------+
1 row in set (0.00 sec)

Upvotes: 1

I think your group by mess up your desired outcome. I worked on your given database schema and cretaed a fiddle and managed to get your desired outcome. So your sql should be something like this:

SELECT * FROM tbl_product_checkout as pc 
LEFT JOIN tbl_product_checkout_status as cs ON 
cs.product_checkout_id = pc.product_checkout_id 
WHERE pc.user_id = 1 ORDER BY cs.checkout_status_id DESC limit 1

By using limit 1, you will get last row as we ordered by DESC.

Keep in mind that i removed date part since there was no date on your example code.

Check Out Fiddle

Upvotes: 1

Related Questions