先入れ先出しモードで2つのテーブル(購入と販売)から損益を取得するmysql計算
P粉296080076
P粉296080076 2023-09-03 11:46:30
0
1
2624

如何从这两个表计算利润或损失?

create table items(id int primary key auto_increment, name varchar(255)); insert into items value(null,'A'); insert into items value(null,'B'); insert into items value(null,'C'); insert into items value(null,'D');
id 姓名
1 一个
2 B
3 C
4 D
create table purchase(id int primary key auto_increment, item_id int,qnt int,price int); insert into purchase value(null,1,10,20); insert into purchase value(null,2,10,22); insert into purchase value(null,3,10,25); insert into purchase value(null,4,10,18); insert into purchase value(null,5,10,25);
id item_id qnt 成本
1 1 10 20
2 2 10 10
3 3 10 10
4 4 10 10
5 1 10 25
6 2 10 16
create table sales(id int primary key auto_increment, item_id int,qnt int,price int); insert into purchase value(null,1,2,25); insert into purchase value(null,2,3,15); insert into purchase value(null,1,3,26); insert into purchase value(null,1,2,22);
id item_id qnt 价格
1 1 2 25
2 2 3 15
3 1 3 26
4 1 2 22

我需要的是将采购表中每件商品的成本像这样,(salespriceqnt)-( costpriceqnt) 作为利润/损失

id item_id qnt 价格 成本 利润
1 1 2 25 40 10
2 2 3 15 30 15
3 1 3 26 60 18
4 1 2 22 40 4

不知道该怎么做,现在我正在使用 PHP 来做这件事,这需要太多时间。

P粉296080076
P粉296080076

全員に返信 (1)
P粉893457026

私が理解している範囲であなたの質問に直接答えるには、このクエリでうまくいくはずです

リーリー

これにより、次の出力が生成されます

###プロジェクト### ###利益### A 172 45 0 0
総売上高 total_purchases
200 -28 B
220 -175 C
250 -250 D
180 -180
いいねを押す+0
    最新のダウンロード
    詳細>
    ウェブエフェクト
    公式サイト
    サイト素材
    フロントエンドテンプレート
    私たちについて 免責事項 Sitemap
    PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!