あなたの質問を破棄すれば、何が起きているのかを見ることができます。
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)
ごめんなさいしかし、質問は何ですか? –
あなたはすべての行を表示するwntですか? –
私の質問を理解していただければ幸いです。ありがとう –