如何使mysql join包含空值?

时间:2017-08-03 06:38:14

标签: mysql

此查询工作正常

   SELECT `category`
          , `product`
          , `(SUM(CASE WHEN stock.is_out = false and stocktrxn.is_defective = false
                       THEN qty 
                       ELSE 0 END) 
            - SUM(CASE WHEN stock.is_out = true 
                       THEN qty 
                       ELSE 0 END))` AS `qty_available`
          , `(SUM(CASE WHEN stock.is_out = false a`nd stocktrxn.is_defective = true THEN qty ELSE 0 END))` AS `qty_defective`
          , `(SUM(CASE WHEN stock.is_out = false THEN qty ELSE 0 END) - SUM(CASE WHEN stock.is_out = true THEN qty ELSE 0 END))` AS `qty_sum` 
     FROM `product` 
LEFT JOIN `stocktrxn` 
       ON `product`.`id` = `stocktrxn`.`product_no`
LEFT JOIN `stock` 
       ON `stock`.`tra_no` = `stocktrxn`.`tra_no `
    GROUP BY `stock`.`warehouse_id`, `product` 
    ORDER BY `category`, `product_name`, `product_type`, `dqty1`, `dqty2`

但是一旦我添加了stockwarehouse_id = :warehouse_id我最终得到的产品没有省略交易。

添加where的正确方法是什么?

0 个答案:

没有答案