相关子查询中的 MySQL LIMIT

发布于 2024-09-14 19:12:47 字数 1396 浏览 6 评论 0原文

我有一个相关子查询,它将返回数量列表,但我需要最高数量,而且只需要最高数量。因此,我尝试引入 order by 和 LIMIT 1 来实现此目的,但 MySQL 抛出一个错误,指出它还不支持子查询中的限制。关于如何解决这个问题有什么想法吗?

SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost,
        FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price
FROM `Product`
    JOIN `ProductOption`
        ON Product.idProduct = ProductOption.Product_idProduct
    JOIN `ProductOptionTier` AS a
        ON a.ProductOption_idProductOption = ProductOption.idProductOption
    JOIN `PaperSize`
        ON PaperSize.idPaperSize = ProductOption.PaperSize_idPaperSize
    JOIN `SheetSize`
        ON SheetSize.PaperSize_idPaperSize = PaperSize.idPaperSize
    JOIN `FinishOption`
        ON FinishOption.Product_idProduct = Product.idProduct
    JOIN `FinishType`
        ON FinishType.idFinishType = FinishOption.Finishtype_idFinishType
    JOIN `FinishTierPrice`
        ON FinishTierPrice.FinishOption_idFinishOption = FinishOption.idFinishOption
WHERE Product.idProduct = 1
    AND FinishTierPrice.idFinishTierPrice IN (SELECT FinishTierPrice.idFinishTierPrice
                                              FROM `FinishTierPrice` 
                                              WHERE FinishTierPrice.Qty <= a.Qty
                                              ORDER BY a.Qty DESC
                                              LIMIT 1)

I have a correlated subquery that will return a list of quantities, but I need the highest quantity, and only the highest. So I tried to introduce an order by and a LIMIT of 1 to achieve this, but MySQL throws an error stating it doesn't yet support limits in subqueries. Any thoughts on how to work around this?

SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost,
        FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price
FROM `Product`
    JOIN `ProductOption`
        ON Product.idProduct = ProductOption.Product_idProduct
    JOIN `ProductOptionTier` AS a
        ON a.ProductOption_idProductOption = ProductOption.idProductOption
    JOIN `PaperSize`
        ON PaperSize.idPaperSize = ProductOption.PaperSize_idPaperSize
    JOIN `SheetSize`
        ON SheetSize.PaperSize_idPaperSize = PaperSize.idPaperSize
    JOIN `FinishOption`
        ON FinishOption.Product_idProduct = Product.idProduct
    JOIN `FinishType`
        ON FinishType.idFinishType = FinishOption.Finishtype_idFinishType
    JOIN `FinishTierPrice`
        ON FinishTierPrice.FinishOption_idFinishOption = FinishOption.idFinishOption
WHERE Product.idProduct = 1
    AND FinishTierPrice.idFinishTierPrice IN (SELECT FinishTierPrice.idFinishTierPrice
                                              FROM `FinishTierPrice` 
                                              WHERE FinishTierPrice.Qty <= a.Qty
                                              ORDER BY a.Qty DESC
                                              LIMIT 1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-09-21 19:12:47

这是经常出现的greatest-n-per-group问题的变体。

您需要单行形式的 FinishTierPrice(称为 p1),与 FinishOption 匹配并具有最大数量,但仍小于或等于 ProductOptionTier 的数量。

实现此目的的一种方法是尝试匹配 FinishTierPrice 中具有相同 FinishOption 和更大数量的第二行 (p2)。如果不存在这样的行(使用外连接并测试它是否为 NULL),则 p1 找到的行是最大的。

SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost,
        FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price
FROM `Product`
    JOIN `ProductOption`
        ON Product.idProduct = ProductOption.Product_idProduct
    JOIN `ProductOptionTier` AS a
        ON a.ProductOption_idProductOption = ProductOption.idProductOption
    JOIN `PaperSize`
        ON PaperSize.idPaperSize = ProductOption.PaperSize_idPaperSize
    JOIN `SheetSize`
        ON SheetSize.PaperSize_idPaperSize = PaperSize.idPaperSize
    JOIN `FinishOption`
        ON FinishOption.Product_idProduct = Product.idProduct
    JOIN `FinishType`
        ON FinishType.idFinishType = FinishOption.Finishtype_idFinishType
    JOIN `FinishTierPrice` AS p1
        ON p1.FinishOption_idFinishOption = FinishOption.idFinishOption
        AND p1.Qty <= a.Qty
    LEFT OUTER JOIN `FinishTierPrice` AS p2
        ON p2.FinishOption_idFinishOption = FinishOption.idFinishOption
        AND p2.Qty <= a.Qty AND (p2.Qty > p1.Qty OR p2.Qty = p1.Qty 
            AND p2.idFinishTierPrice > p1.idFinishTierPrice)
WHERE Product.idProduct = 1
    AND p2.idFinishTierPrice IS NULL

This is a variation of the greatest-n-per-group problem that comes up frequently.

You want the single row form FinishTierPrice (call it p1), matching the FinishOption and with the greatest Qty, but still less than or equal to the Qty of the ProductOptionTier.

One way to do this is to try to match a second row (p2) from FinishTierPrice that would have the same FinishOption and a greater Qty. If no such row exists (use an outer join and test that it's NULL), then the row found by p1 is the greatest.

SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost,
        FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price
FROM `Product`
    JOIN `ProductOption`
        ON Product.idProduct = ProductOption.Product_idProduct
    JOIN `ProductOptionTier` AS a
        ON a.ProductOption_idProductOption = ProductOption.idProductOption
    JOIN `PaperSize`
        ON PaperSize.idPaperSize = ProductOption.PaperSize_idPaperSize
    JOIN `SheetSize`
        ON SheetSize.PaperSize_idPaperSize = PaperSize.idPaperSize
    JOIN `FinishOption`
        ON FinishOption.Product_idProduct = Product.idProduct
    JOIN `FinishType`
        ON FinishType.idFinishType = FinishOption.Finishtype_idFinishType
    JOIN `FinishTierPrice` AS p1
        ON p1.FinishOption_idFinishOption = FinishOption.idFinishOption
        AND p1.Qty <= a.Qty
    LEFT OUTER JOIN `FinishTierPrice` AS p2
        ON p2.FinishOption_idFinishOption = FinishOption.idFinishOption
        AND p2.Qty <= a.Qty AND (p2.Qty > p1.Qty OR p2.Qty = p1.Qty 
            AND p2.idFinishTierPrice > p1.idFinishTierPrice)
WHERE Product.idProduct = 1
    AND p2.idFinishTierPrice IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文