gpt4 book ai didi

mysql - 获取库存余额

转载 作者:行者123 更新时间:2023-11-29 08:55:26 26 4
gpt4 key购买 nike

我有如下 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;

--转储表产品的数据

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 数据类型设置为相同是一个好主意。它是 products 中的 varcharsales/stock 中的 int

我做了一个Fiddle为你

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com