如何使用左连接mysql从表2获取最后一行

时间:2017-12-16 09:30:32

标签: mysql

我有两个表tbl_product_checkouttbl_product_checkout_status,我希望从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

我尝试使用以下查询

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

但上面查询的输出是,

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

但我希望结果为,

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

2 个答案:

答案 0 :(得分:1)

我认为你的小组搞砸了你想要的结果。我研究了你给定的数据库模式,创造了一个小提琴,并设法得到你想要的结果。所以你的sql应该是这样的:

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

通过使用限制1,您将获得DESC订购的最后一行。

请记住,我删除了日期部分,因为您的示例代码上没有日期。

Check Out Fiddle

答案 1 :(得分:1)

添加where = max子查询,例如

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
)
;

结果

+---------------------+---------+---------------------------+--------------------+---------------------+-----------------------+
| 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)