gpt4 book ai didi

sql - 如何对四个表使用内连接查询

转载 作者:行者123 更新时间:2023-12-04 05:52:18 24 4
gpt4 key购买 nike

我的数据库中有四个表( 笔记 费用 类别 7 项 | 92019 |

项目 表将在特定类别 id 上有这么多项目
项目表有字段
id(primary key), item name(text), unit(text), category id(foreign key)
类别 表将只有两个字段
类别表有字段
id(primary key), cat_id(integer), category name(text) .

备注 table 用于记录每个购物笔记。
笔记表有字段
note_id(primary key), date(DATE), total cost(integer) .

费用表用于存储购买的物品及其对应的数量和价格在特定的购物笔记下,使用note id作为外键
费用表有字段
id(primary key), note_id(foreign key), quantity(integer), price(integer), item_name(text)
当我输入起始日期、截止日期和特定类别时,我需要在起始日期和截止日期之间购买的该类别下的项目。
我需要一个查询,将输出为:

ITEM NAME TOTALQTY TOTALPRICE
carrot 5kg 500

谁能帮我解决一下??

最佳答案

尝试这个

select A.name, sum(C.unit), A.unit, sum(C.price) 
from items as A
INNER JOIN categories as B ON A.category=B._id
INNER JOIN expenses as C ON A.name=C.item_name
INNER JOIN notes as D ON C.Note_id=D._id
where
D.date1 between '2012-01-01' and '2012-03-31' and B.name='Vegetables'
group by
A.name, A.unit

关于sql - 如何对四个表使用内连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9937264/

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