所以,我试图创建一个数据库,允许在客户订单或我在供应商处订购时自动添加和减去产品数量。这是我的代码:
select p.productid, p.productname,p.price,
p.quantity + (ifnull(sum(s.quantity),0) - ifnull(sum(o.quantity),0))as
quantity
from product p
left join wholesale.supply s on p.productname = s.productname
left join wholesale.order o on p.productname = o.productname
group by p.productname order by quantity
这是没有任何订单的输出:
productid productname price quantity
6 Sony X 19700 6
4 Samsung Galaxy S8 38000 11
5 LG G6 31500 12
这是我的供应商页面中的输出
id productname quantity
1 Sony X 10
2 Sony X 10
3 Sony X 10
4 Sony X 10
这是在订单页面
id productname quantity
1 Sony X 10
2 Sony X 5
输出现在是:
productid productname price quantity
6 Sony X 19700 26
4 Samsung Galaxy S8 38000 11
5 LG G6 31500 12
如果你算数学索尼X应该是:
productid productname price quantity
6 Sony X 19700 31
4 Samsung Galaxy S8 38000 11
5 LG G6 31500 12
我的问题是,数量的值不可靠,就像在示例中一样, 我的股票中有Sony X 6pcs。 我总共订购了40件。 并且客户订购总共15件 它应该是31,因为6 + 40 = 46-15 = 31 但它在我的数据库中显示了26个。
答案 0 :(得分:0)
如果我们打破您的查询,我们可以看到发生了什么。
drop table if exists p;
create table p (productid int, productname varchar(20), price int, quantity int);
insert into p values
( 6 , 'Sony X' , 19700 , 6),
( 4 , 'Samsung Galaxy S8' , 38000 , 11),
( 5 , 'LG G6' , 31500 , 12);
drop table if exists s;
create table s (id int, productname varchar(20), quantity int);
insert into s values
( 1 , 'Sony X' , 10),
( 2 , 'Sony X' , 10),
( 3 , 'Sony X' , 10),
( 4 , 'Sony X' , 10);
drop table if exists o;
create table o(id int, productname varchar(20), quantity int);
insert into o values
(1 , 'Sony X' , 10),
(2 , 'Sony X' , 5);
MariaDB [sandbox]> select p.productid, p.productname,p.price,
-> p.quantity
-> ,ifnull(sum(s.quantity),0) squantity
-> ,ifnull(sum(o.quantity),0) oquantity
-> from p
-> left join s on p.productname = s.productname
-> left join o on p.productname = o.productname
-> group by p.productname
-> order by quantity
-> ;
+-----------+-------------------+-------+----------+-----------+-----------+
| productid | productname | price | quantity | squantity | oquantity |
+-----------+-------------------+-------+----------+-----------+-----------+
| 6 | Sony X | 19700 | 6 | 80 | 60 |
| 4 | Samsung Galaxy S8 | 38000 | 11 | 0 | 0 |
| 5 | LG G6 | 31500 | 12 | 0 | 0 |
+-----------+-------------------+-------+----------+-----------+-----------+
3 rows in set (0.00 sec)
你可以试试这个
MariaDB [sandbox]> select p.productid,p.productname,p.price,p.quantity, s.*,o.*,p.quantity + ifnull(squantity,0) - ifnull(oquantity,0) qty
-> from p
-> left join
-> (select s.productname sproductname, sum(s.quantity) squantity from s group by s.productname) s on sproductname = p.productname
-> left join
-> (select o.productname oproductname, sum(o.quantity) oquantity from o group by o.productname) o on oproductname = p.productname;
+-----------+-------------------+-------+----------+--------------+-----------+--------------+-----------+------+
| productid | productname | price | quantity | sproductname | squantity | oproductname | oquantity | qty |
+-----------+-------------------+-------+----------+--------------+-----------+--------------+-----------+------+
| 6 | Sony X | 19700 | 6 | Sony X | 40 | Sony X | 15 | 31 |
| 4 | Samsung Galaxy S8 | 38000 | 11 | NULL | NULL | NULL | NULL | 11 |
| 5 | LG G6 | 31500 | 12 | NULL | NULL | NULL | NULL | 12 |
+-----------+-------------------+-------+----------+--------------+-----------+--------------+-----------+------+
3 rows in set (0.00 sec)