子表问题 - MySql查询

时间:2017-05-02 04:07:40

标签: mysql mariadb

我在下面的查询中遇到问题,销售订单项是销售订单表中的子表,那些有多行的表(销售订单项)只显示第一项我的意思是第一行并且没有显示所有其他行。请放弃一些轻松的人。

select 
 `tabSales Order`.`name` as "Sales Order:Link/Sales Order:120",
`tabSales Order`.`customer` as "Customer:Link/Customer:120",
 `tabSales Order Item`.item_code as "Item:Link/Item:120",
`tabSales Order Item`.item_name as "Des:Link/Item:120",
`tabSales Order Item`.qty as "Qty:Int:100",
 `tabSales Order Item`.delivered_qty as "Delivered Qty:Int:100",
 (`tabSales Order Item`.qty - ifnull(`tabSales Order Item`.delivered_qty, 0)) as "Qty to Deliver:Int:140",
`tabSales Order Item`.date as "Sch date:Date:100",
`tabItem`.thumbnail as "Cut wt:Data:60",
(`tabSales Order Item`.qty * `tabItem`.thumbnail) as "Tonnage:Float:80",
`tabProduct master`.grade as "Grade:Data:100",
`tabProduct master`.dieno as "Die no:Data:100",
`tabProduct master`.hammer as "Hammer:Data:50",
`tabProduct master`.rm_idl_cs as "Idl sec:Data:60",
`tabProduct master`.rm_alt_cs as "Alt sec:Data:60",
ifnull(SUM(CASE WHEN `tabSales Order`.`name` = `tabProduction Order`.`sales_order` and `tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
         THEN `tabProduction Order`.`qty`
    END),0) as "Issued:Int:80",
SUM(CASE WHEN `tabSales Order`.`name` = `tabProduction Order`.`sales_order` and `tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
         THEN `tabProduction Order`.`produced_qty`
    END) as "Cut:Int:80",
(`tabSales Order Item`.qty - ifnull(SUM(CASE WHEN `tabSales Order`.`name` = `tabProduction Order`.`sales_order` and `tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
         THEN `tabProduction Order`.`qty`
    END),0)) as "To be issued:Int:80"

    from  `tabSales Order`
    LEFT JOIN  `tabSales Order Item`  ON ( `tabSales Order Item`.`parent` = `tabSales Order`.`name`
              and  `tabSales Order`.docstatus = 1  )
    LEFT JOIN  `tabItem`  ON `tabItem`.`item_code` = `tabSales Order Item`.`item_code`
    LEFT JOIN  `tabProduct master`  ON `tabProduct master`.`part_no` = `tabSales Order Item`.`item_code`
    LEFT JOIN  `tabProduction Order`
               ON (`tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
              and  `tabSales Order`.`name` = `tabProduction Order`.`sales_order` )
    where  `tabSales Order`.status not in ("Stopped", "Closed", "Completed" )
      and  ifnull(`tabSales Order Item`.delivered_qty,0) < ifnull(`tabSales Order Item`.qty,  0 )
    GROUP BY  `tabSales Order`.`name`
    HAVING  (`tabSales Order Item`.qty -
                SUM(CASE WHEN `tabSales Order`.`name` = `tabProduction Order`.`sales_order`
                      and  `tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
                         THEN `tabProduction Order`.`qty` END)
            ) > 0
      or  (`tabSales Order Item`.qty -
                SUM(CASE WHEN `tabSales Order`.`name` = `tabProduction Order`.`sales_order`
                      and  `tabSales Order Item`.`item_code` = `tabProduction Order`.`production_item`
                         THEN `tabProduction Order`.`qty` END)
            ) is NULL;

1 个答案:

答案 0 :(得分:0)

首先,缩短它以使其更具可读性:

    FROM  `tabSales Order` AS so
    LEFT JOIN  `tabSales Order Item` AS soi
               ON ( soi.`parent` = so.`name`
              AND  so.docstatus = 1  )
    LEFT JOIN  `tabItem` AS i  ON i.`item_code` = soi.`item_code`
    LEFT JOIN  `tabProduct master` AS pm
               ON pm.`part_no` = soi.`item_code`
    LEFT JOIN  `tabProduction Order` AS po
               ON (soi.`item_code` = po.`production_item`
              AND  so.`name` = po.`sales_order` )
    WHERE  so.status NOT IN ("Stopped", "Closed", "Completed" )
      AND  IFNULL(soi.delivered_qty,0) < IFNULL(soi.qty,  0 )
    GROUP BY  so.`name`
    HAVING  IFNULL(soi.qty -
                SUM(CASE WHEN so.`name` = po.`sales_order`
                   AND  soi.`item_code` = po.`production_item`
                         THEN po.`qty` END), 1) > 0

然后进行一些一般分析,不一定解决问题:

  • 你真的需要LEFT吗?也就是说,&#34;对&#34;表格可选,你想要NULLs吗?
  • LEFT JOIN b ON b.x = 1LEFT JOIN b WHERE b.x = 1之间存在差异。前者可能包含比后者更多的b行。
  • 在许多情况下NULLs都很好,但是如果你能避免它们,那么SQL会变得更简单。

更多

首先运行它而不使用&#34; GROUP BY tabSales Ordername&#34;。你会得到你遗漏的线条,但你可能会得到比你想要的更多。如果是这样,那么删除其余的联接,让我们看看我们是否可以在没有“内爆”的情况下将其添加回来。物品。

为了帮助理解数据,请说明每个关系是1:1还是1:很多或很多:很多。