gpt4 book ai didi

mysql - Sql 连接三表

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

这是我的表格..我想要这个输出:

pid | pname | custname | quantity | total base price | sale price | profit

架构:

create table customers
(
cid int,
cname varchar(1000),
cg varchar(1000)
)

create table prod
(
pid int,
pname varchar(1000),
baseprice int,
saleprice int
)

create table orders
(
oid int,
custid int,
pid int,
quantity int,
odate date
)

如何为此编写查询?

最佳答案

--pid | pname | custname | quantity | total base price | sale price | profit
select o.pid, p.pname, c.cname AS custname, SUM(o.quantity) AS quantity,
SUM(p.baseprice) AS 'total base price', SUM(p.saleprice) AS 'sale price',
SUM(p.baseprice) - SUM(p.saleprice) AS profit -- change this math to what you need
from orders o join prod p
on o.pid = p.pid
join customers c
on o.custid = c.cid
group by o.pid, p.pname, c.cname

关于mysql - Sql 连接三表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38327753/

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