0

我有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

通过上述查询,我​​没有得到正确的平衡。能否请你帮忙。

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

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(或架构更改)以获得更好的速度/性能来获得库存平衡?

4

1 回答 1

2

首先,添加p.product_nameGROUP 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和里面intsales/stock

我为你做了一个小提琴

于 2012-04-09T19:46:24.877 回答