【问题标题】:MYSQL How find order products matching current storage products (compare two tables for 100% match)MYSQL如何查找与当前存储产品匹配的订单产品(比较两个表100%匹配)
【发布时间】:2020-12-05 16:48:47
【问题描述】:

我正在寻找解决方案来检查一个表中的多行是否在另一个表中匹配。在我的情况下,我需要检查订单中的物品是否在存储中。目前我使用php来检查订单-脚本正在接受未结订单并逐一检查存储。它产生了很多查询,而且效率不高,我正在寻找通过 sql 查询执行此操作的解决方案。

期望的结果应该是:

OrderId | Date       | Products 

1002/02 | 2020/08/16 | 1x Ipod; 2x battery; 9x some item;

0333/4  | 2020/06/22 | 10x shelf

存储产品表

id | id_product | quantity

订单

id | reference | id_status | created_at

订购产品

Id | id_order | quantity | id_storage_product

我已经编写了一些代码来生成上面可见的表格,但结果它甚至没有接近预期。

select('orders.id', orders.created_at','orders.reference', 'storage_products.id as storageProductId')
                ->join('order_products', 'orders.id', '=', 'order_products.id_order')
                ->join('storage_products', 'order_products.id_product', '=', 'storage_products.id_product')
                ->where('storage_products.quantity', '>=', 'order_products.quantity')
                ->whereIn('orders.id_status', array(1, 2))  //get new orders/ open
                ->where('order_products.id_storage_product', null) 
                ->groupBy('orders.id');

清理sql:

SELECT `orders`.`id`,
       `orders`.`created_at`,
       `orders`.`reference`,
       `storage_products`.`id` AS `storageProductId`,
       `order_products`.`id_order`
FROM   `orders`
       INNER JOIN `order_products`
               ON `orders`.`id` =
                  `order_products`.`id_order`
       INNER JOIN `storage_products`
               ON `order_products`.`id_product` =
                  `storage_products`.`id_product`
WHERE  `storage_products`.`quantity` >=
              'order_products.quantity'
       AND `orders`.`id_status` IN ( 1, 2 )
       AND `order_products`.`id_storage_product` IS NULL
GROUP  BY `orders`.`id`
ORDER  BY `orders`.`id` ASC

所以代码应该找到未结订单(id_status);存储数量等于或大于订单产品;其中 id_storage_products 为空(表示在网站上购买的产品,但在订购时并未存储)。 上面的查询结果是错误的,因为它显示我与存储部分匹配 - 即使没有检查数量(有些产品有 0 但仍然显示)。

非常感谢您的帮助

编辑:小提琴样本:https://www.db-fiddle.com/f/6jKvKXPYvsLeXgm3Qv1nHu/0

【问题讨论】:

标签: mysql laravel


【解决方案1】:

我在 db-fiddle 链接上尝试了以下查询,这很有效。

SELECT
    orders.reference, orders.created_at, order_products.id_product
FROM
    storage_products 
LEFT JOIN 
    order_products ON storage_products.id_product = order_products.id_product 
LEFT JOIN
    orders ON orders.id = order_products.id_order;

我在查询中所做的是在 order_products 中调用具有相同 id_product 的所有 storage_products,然后继续调用被调用的 order_products 中的所有订单。

【讨论】:

    【解决方案2】:

    您的查询包含以下条件:

    AND `order_products`.`id_storage_product` IS NULL
    

    但在您的示例数据中,所有值都是0
    所以我改为使用COALESCE() 来涵盖这两种情况。
    我也删除了条件:

    AND `orders`.`id_status` IN ( 1, 2 )
    

    因为id_status 列未包含在您的示例数据中表orders 的定义中。

    此查询有效:

    SELECT o.id,
           o.reference,
           o.created_at,
           GROUP_CONCAT(op.quantity, 'x', op.id_product separator ' ;') products
    FROM orders o
    INNER JOIN order_products op ON o.id = op.id_order
    INNER JOIN storage_products sp ON op.id_product = sp.id_product
    WHERE sp.quantity >= op.quantity AND COALESCE(op.id_storage_product, 0) = 0
    GROUP  BY o.id, o.reference, o.created_at
    ORDER  BY o.id ASC
    

    请参阅demo
    结果:

    | id  | reference | created_at          | products      |
    | --- | --------- | ------------------- | ------------- |
    | 2   | 345554/02 | 2020-08-22 00:00:00 | 3x188 ; 1x155 |
    

    如果你也加入表products(我假设有这样一个表)你可以得到产品的名称而不是它们的ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      相关资源
      最近更新 更多