我有3张桌子,如下所示:

products:[product_id,product_name]
stock:[product_id,date,qty_received]
sales:[product_id,date,qty_sold]


我正在尝试通过减去qty_received-qty_sold来显示所有产品的余额。
我的查询是:

SELECT DISTINCT p.product_id, p.product_name,
IFNULL(sum(qty_received),0)-IFNULL(sum(qty_sold),0) as balance FROM products p
LEFT JOIN stock st ON (p.product_id=st.product_id)
LEFT JOIN sales sa ON (p.product_id=sa.product_id)
GROUP BY p.product_id


通过上述查询,我​​无法获得正确的余额。
能否请你帮忙。

附加信息:
以下是我的表的简化结构
产品表:

CREATE TABLE IF NOT EXISTS `products` (
  `product_id` varchar(15) NOT NULL,
  `product_name` varchar(20) NOT NULL,
  UNIQUE KEY `product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


-转储表products的数据

INSERT INTO `products` (`product_id`, `product_name`) VALUES
  ('111', 'Product One'),
  ('222', 'Product Two'),
  ('333', 'Product Three'),
  ('444', 'Product Four');


库存表:

CREATE TABLE IF NOT EXISTS `stock` (
  `stock_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `qty_received` int(11) NOT NULL,
  PRIMARY KEY (`stock_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=112 ;


-转储表stock的数据

INSERT INTO `stock` (`stock_id`, `product_id`, `qty_received`) VALUES
  (1, 111, 5),
  (2, 222, 10),
  (3, 333, 4),
  (4, 444, 6),
  (5, 111, 2),
  (6, 222, 7),
  (7, 111, 3),
  (8, 111, 3);


销售表

CREATE TABLE IF NOT EXISTS `sales` (
  `sales_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `qty_sold` int(11) NOT NULL,
  PRIMARY KEY (`sales_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;


-转储表sales的数据

INSERT INTO `sales` (`sales_id`, `product_id`, `qty_sold`) VALUES
  (1, 111, -1),
  (2, 222, -3),
  (3, 333, -2),
  (4, 111, -3),
  (5, 222, -1),
  (6, 222, -4);


我得到的结果是:

+------------+--------------+---------+
| product_id | product_name | balance |
+------------+--------------+---------+
| 111        | Product One  | 10      |
| 222        | Product Two  | 35      |
| 333        | Product Three| 2       |
| 444        | Product Four | 6       |
+------------+--------------+---------+


我期望正确的平衡是

+------------+--------------+---------+
| product_id | product_name | balance |
+------------+--------------+---------+
| 111        | Product One  | 9       |
| 222        | Product Two  | 9       |
| 333        | Product Three| 2       |
| 444        | Product Four | 6       |
+------------+--------------+---------+


最终得到了适用于该解决方案的可运行的sql,但不确定这是否是执行此操作的最佳方法。我非常担心性能,因为这些表有大量记录。

SELECT p.product_id,p.product_name,
IFNULL(sum(st.qty_received),0)+IFNULL(sold,0) as balance FROM products p
  LEFT JOIN stock st ON (p.product_id=st.product_id)
  LEFT JOIN (SELECT sa.product_id,sum(sa.qty_sold) as sold FROM sales sa
     GROUP BY sa.product_id) as t1 ON (t1.product_id=p.product_id)
GROUP BY product_id


任何人都可以建议使用更好的sql(或更改架构)以提高速度/性能,以达到库存平衡?

最佳答案

首先,在p.product_name子句中添加GROUP BY

其次,删除DISTINCT-GROUP BY不必要。

更新

我尝试了几次查询,发现这看起来最简单:

SELECT
  p.product_id,
  p.product_name,
  COALESCE(SUM(qty), 0) as balance
FROM products p
LEFT JOIN (
    SELECT product_id, qty_received AS qty FROM stock st UNION ALL
    SELECT product_id, qty_sold FROM sales st
) AS T
  ON p.product_id = T.product_id
GROUP BY
  p.product_id,
  p.product_name


另外,我认为使所有表中的product_id数据类型相同是个好主意。在varchar中是products,在int中是sales/stock

我为你做了一个Fiddle

关于mysql - 获得库存余额,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10078788/

10-16 13:35