Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 我在SQL推断查询中得到了不同的结果_Mysql_Sql - Fatal编程技术网

Mysql 我在SQL推断查询中得到了不同的结果

Mysql 我在SQL推断查询中得到了不同的结果,mysql,sql,Mysql,Sql,我的查询应该返回订单和销售之间的差异(订单销售)。它应该得到库存订单中的订单总数量和每种产品的销售总数量,并得到两者之间的差额以得到剩余数量 范例 产品1:(3+7)-179=-169 产品2:(150)-(111+30)=9 表: |---------------------------------------------| | inventories_order | |------------------------------------

我的查询应该返回订单和销售之间的差异(订单销售)。它应该得到库存订单中的订单总数量和每种产品的销售总数量,并得到两者之间的差额以得到剩余数量

范例

  • 产品1:(3+7)-179=-169
  • 产品2:(150)-(111+30)=9
  • 表:

    |---------------------------------------------|
    |             inventories_order               |
    |---------------------------------------------|
    |      invent_id      |     order_quantity    |
    |---------------------|-----------------------|
    |          1          |         3             |
    |---------------------|-----------------------|
    |          2          |         150           |
    |---------------------|-----------------------|
    |          1          |         7             |
    |---------------------|-----------------------|
    
    |---------------------------------------------|
    |                   sales                     |
    |---------------------------------------------|
    |      invent_id      |     quantity          |
    |---------------------|-----------------------|
    |          1          |         179           |
    |---------------------|-----------------------|
    |          2          |         111           |
    |---------------------|-----------------------|
    |          2          |         30            |
    |---------------------|-----------------------|
    
    |---------------------------------------------|
    |                 inventories                 |
    |---------------------------------------------|
    |      invent_id      |     product_name      |
    |---------------------|-----------------------|
    |          1          |         product1      |
    |---------------------|-----------------------|
    |          2          |         product2      |
    |---------------------|-----------------------|
    
    
      String selectQuery = "SELECT product_name, ((sum(IFNULL(o.order_quantity,0))) - ((sum(s.quantity)))"
                + " ) as quantity_left "
                + " FROM inventories i left join inventories_order o on i.invent_id=o.invent_id "
                + " left join sales s on i.invent_id=s.invent_id "
                + " group by i.invent_id order by quantity_left desc ";
    

    您已加入您的查询。当您有一个连接,它不是唯一的父子(1对多)之间的连接,那么求和结果将永远不可靠。首先,请执行以下操作:

    SELECT IFNULL(o.orderQty,0)-ifnull(s.saleQty,0) as quantity_left 
    FROM inventories i 
    LEFT JOIN 
    (SELECT invent_id, SUM(order_quantity) AS orderQty
     FROM inventories_order
     group by invent_id) o on i.invent_id=o.invent_id
    LEFT JOIN 
    (SELECT invent_id,SUM(quantity) AS saleQty
      FROM sales
      group by invent_id) s on i.invent_id=s.invent_id
    ORDER by quantity_left desc
    

    您可以在
    inventory\u order
    sales
    表上使用
    UNION ALL

  • grp
    1
    inventory\u订单中
    平均正数
  • grp
    -1
    sales
    中表示负数
  • 使用
    Outer JOIN
    inventory
    一起使用
    SUM
    和简单的乘法进行运算

    TestDLL

    CREATE TABLE inventories_order(
      invent_id  INT, 
      order_quantity INT
    );
    
    INSERT INTO inventories_order VALUES 
    (1,3), (2,150), (1,7);
    
    CREATE TABLE sales(
      invent_id  INT, 
      quantity INT
    );
    
    INSERT INTO sales VALUES
    (1,179), (2,111), (2,30);
    
    CREATE TABLE inventories(
      invent_id  INT, 
      product_name VARCHAR(50)
    );
    
    INSERT INTO inventories VALUES (1,'product1'), (2,'product2');
    
    查询

    select t1.product_name,IFNULL(sum(num*grp),0) as quantity_left
    from 
    (
      SELECT 1 grp,order_quantity as num,invent_id FROM inventories_order 
      UNION ALL 
      SELECT -1,quantity,invent_id FROM sales   
    )t 
    RIGHT JOIN inventories t1 on t.invent_id= t1.invent_id
    GROUP BY t1.product_name
    
    [结果]

    | product_name | quantity_left |
    |--------------|---------------|
    |     product1 |          -169 |
    |     product2 |             9 |
    

    能否提供一些样本数据和预期结果?格式化而不是图像。真的吗help@D-施:首先,我为这个模糊的问题道歉。我提供了更多的信息以使其更清楚。谢谢你指出这一点。我补充一个答案,你可以试试@RogerYou已经有了答案。是否缺少一些主键?你的答案很好,我能够将其融入到我的问题中。然而,我没有得到-169和9,而是得到了-170和8。为什么省略了1个数字?@Roger,可能你的数据有不同的值。我得到-169和9。这是指向sqlFiddle示例的链接:@True,我将查看它。非常感谢你的帮助,也谢谢你的回答。非常感谢。