gpt4 book ai didi

mysql - 关系代数银行数据库

转载 作者:行者123 更新时间:2023-11-29 11:02:37 24 4
gpt4 key购买 nike

我有一个银行数据库的关系模式

Customer(custid PK, name, city, streetaddr, province)

Account(acctid PK, custid, atype, startdate, balance, branchid), (custid ref Customer, branchid ref Branch)

Branch(branchid PK, mgrid, city, streetaddr, province), (mgrid ref Employees.empid)

Employees(empid PK, name, branchid, salary, city, streetaddr, province), (branchid ref Branch)

Transactions(tid PK, acctid, transtype, transdate, transamount, branchid) (acctid ref Account, branchid ref Branch)

I'm trying to find all "savings" account ids, name of the customer, and customer ids, of customers who only made one transaction this year

显然,我需要使用“交易”、“帐户”和“客户”表。我加入了“交易账户”以查看已进行一笔或多笔交易的所有账户,但我在查找“只有一笔”交易的规范时遇到了麻烦。我觉得我需要使用设置差异,但我仍然无法想清楚。

最佳答案

可能是这样的:

select a.acctid, c.custid, c.name
from account a
inner join (
select acctid
from transactions
where year(transdate) = year(curdate())
group by acctid
having count(tid) = 1
) t on a.acctid = t.acctid
inner join customer c
on a.custid = c.custid
where a.atype = 'savings';

关于mysql - 关系代数银行数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41949019/

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