将子查询结果集限制为最后一条记录

时间:2019-04-11 19:17:34

标签: php mysql

我有四个链接的表。每个购物车项目的itemStatus表可以有多个记录,以显示其历史记录(例如,购买,处理,运输,交付)。商店表中输入购物车项目名称。订单表定义了订单中的所有购物车项目。

要显示订单中每个购物车项目的最新状态(不是整个历史记录),我只需要获取itemStatus的最后一条记录。我正在使用子查询来获取itemStatus表的记录,但无法弄清楚如何获取最后一条记录。玩下面的代码,我最终总是得到第一条记录:

SELECT
    tblOrders.id,tblOrders.status as orderStatus,tblOrders.created,tblCart.id AS cartID,tblCart.status AS cartStatus,tblCart.qty,tblCart.price,tblShop.title,tblItem.itemStatus 
FROM
    ".PREFIX."Orders tblOrders 
LEFT JOIN
    ".PREFIX."Cart tblCart ON tblOrders.id=tblCart.OID 
LEFT JOIN
    ".PREFIX."Shop tblShop ON tblCart.PID=tblShop.id 
LEFT JOIN
    (SELECT
        CID,status as itemStatus
    FROM
        ".PREFIX."ItemStatus
    WHERE
        status IN ('pending','refunded','cancelled','purchased','backordered','shipping','delivered')
GROUP BY CID
    ORDER BY
        created DESC
    ) tblItem ON tblCart.id=tblItem.CID 
WHERE
    tblOrders.status<>'disabled' AND tblCart.status='purchased' AND tblCart.BID='".$gbl_user['id']."' 
ORDER BY
    tblOrders.updated DESC

如上所示,我尝试使用'GROUP BY'子句来限制子查询返回的记录数,但是不会产生期望的结果。我也尝试过在子查询中放置'LIMIT 1'子句,但这仅将返回的记录数限制为1(因此任何其他购物车商品都不会以状态结尾)。任何帮助将不胜感激!

更新:

对于重复标记,我尝试使用MAX()语句更新代码,并删除“ ORDER BY”,如下所示:

SELECT
    tblOrders.id,tblOrders.status as orderStatus,tblOrders.created,tblCart.id AS cartID,tblCart.status AS cartStatus,tblCart.qty,tblCart.price,tblShop.title,tblItem.itemStatus 
FROM
    ".PREFIX."Orders tblOrders 
LEFT JOIN
    ".PREFIX."Cart tblCart ON tblOrders.id=tblCart.OID 
LEFT JOIN
    ".PREFIX."Shop tblShop ON tblCart.PID=tblShop.id 
LEFT JOIN
    (SELECT
        CID,status as itemStatus,MAX(created) as itemCreated
    FROM
        ".PREFIX."ItemStatus
    WHERE
        status IN ('pending','refunded','cancelled','purchased','backordered','shipping','delivered')
GROUP BY CID
    ) tblItem ON tblCart.id=tblItem.CID 
WHERE
    tblOrders.status<>'disabled' AND tblCart.status='purchased' AND tblCart.BID='".$gbl_user['id']."' 
ORDER BY
    tblOrders.updated DESC

更新2:

我提供了可以解决该问题的实际代码,但由于某些原因未获批准。我在下面发布了正确的代码:

SELECT
    tblOrders.id, tblOrders.status as orderStatus, tblOrders.created,
    tblCart.id AS cartID, tblCart.status AS cartStatus,tblCart.qty,
    tblCart.price, tblShop.title, tblItem.status as itemStatus 
FROM
    ".PREFIX."Orders tblOrders 
LEFT JOIN
    ".PREFIX."Cart tblCart ON tblOrders.id=tblCart.OID 
LEFT JOIN
    ".PREFIX."Shop tblShop ON tblCart.PID=tblShop.id 
LEFT JOIN
    ".PREFIX."ItemStatus tblItem ON tblCart.id=tblItem.CID 
JOIN (
    SELECT
        CID, MAX(created) AS maxCreated
    FROM
        ".PREFIX."ItemStatus
    WHERE
        status IN ('pending','refunded','cancelled','purchased','backordered','shipping','delivered')
    GROUP BY
        CID
) tblMaxItem ON tblItem.CID=tblMaxItem.CID AND tblItem.created=tblMaxItem.maxCreated
WHERE
    tblOrders.status<>'disabled' AND tblCart.status='purchased' AND tblCart.BID='".$gbl_user['id']."' 
ORDER BY
    tblOrders.updated DESC

1 个答案:

答案 0 :(得分:0)

您需要同时与ItemStatus和一个子查询结合使用,该子查询获取每个项目的最新日期,以便您可以从该行中获取其他列的列。

SELECT
    tblOrders.id, tblOrders.status as orderStatus, tblOrders.created,
    tblCart.id AS cartID, tblCart.status AS cartStatus,tblCart.qty,
    tblCart.price, tblShop.title, tblItem.status AS itemStatus
FROM
    ".PREFIX."Orders tblOrders 
LEFT JOIN
    ".PREFIX."Cart tblCart ON tblOrders.id=tblCart.OID 
LEFT JOIN
    ".PREFIX."Shop tblShop ON tblCart.PID=tblShop.id 
LEFT JOIN
    ".PREFIX."ItemStatus tblItem ON tblCart.id=tblItem.CID 
LEFT JOIN (
    SELECT
        CID, MAX(created) AS maxCreated
    FROM
        ".PREFIX."ItemStatus
    WHERE
        status IN ('pending','refunded','cancelled','purchased','backordered','shipping','delivered')
    GROUP BY
        CID
) tblMaxItem ON tblItem.CID=tblMaxItem.CID AND tblItem.created=tblMaxItem.maxCreated
WHERE
    tblOrders.status<>'disabled' AND tblCart.status='purchased' AND tblCart.BID='".$gbl_user['id']."' 
ORDER BY
    tblOrders.updated DESC