mysql 编写一个查询来确定有多少产品已售出并获利

zbq4xfa0  于 2022-12-03  发布在  Mysql
关注(0)|答案(2)|浏览(110)

我对SQL很陌生,我有三个表,事务,产品和客户,我想知道有多少产品卖出了利润。

SELECT t.product_id, p.id,  sum(t.total_price / t.quantity) - p.price As profit 
From transactions as t , products As p
INNER JOIN transactions
on t.product_id = p.id
GROUP by t.product_id

我的交易数据表中只有总价数据行。我应该将total_price除以quantity还是 *?我的全部查询如何?

sr4lhrrt

sr4lhrrt1#

SELECT t.product_id,sum(t.total_price / t.quantity - p.price) as profit
  FROM transactions as t
     INNER JOIN product as p on t.id = p.id
       GROUP by t.product_id
         order by t.product_id

你能试试这边吗

pjngdqdw

pjngdqdw2#

SELECT t.product_id,sum(t.total_price / t.quantity - p.price) as profit, 
  count(t.quantity) as sold_count
    FROM transactions as t
      INNER JOIN product as p on t.id = p.id
        GROUP by t.product_id
          order by t.product_id

相关问题