Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 关系代数银行数据库_Mysql_Sql_Relational Database_Relational Algebra - Fatal编程技术网

Mysql 关系代数银行数据库

Mysql 关系代数银行数据库,mysql,sql,relational-database,relational-algebra,Mysql,Sql,Relational Database,Relational Algebra,我有一个银行数据库的关系模式 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 Empl

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

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)
我试图找到今年只做过一笔交易的客户的所有储蓄账户ID、客户姓名和客户ID


显然,我需要使用表Transactions、Account和Customer。我将帐户与事务合并,以查看进行了一个或多个事务的所有帐户,但我很难找到仅一个事务的规范。我觉得我需要使用“设置差异”,但我仍然无法仔细思考。

类似的内容可能:

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';
这涉及到自连接。是的,我们“使用集差”

一年内有1笔交易的账户是指有一些交易的账户减去至少有2笔交易的账户。前者正在进行交易。后者是指Transactionstid、acctid、transtype、transdate、transamount、branchid和Transactionstid2、acctid、transtype2、transdate、transamount2、branchid和tid2。Ie在事务中自然连接限制tid tid2重命名tid\tid2、transtype\transtype2、transamount\transamount2、branchid\branchid2事务。您可以将transtype、transamount和branchid投射到减号之前,而不是重命名它们

SQL中可能最简单的方法是按acctid对今年的事务进行分组,并选择counttid>1的组。这是@GurV的答案

但是,我将解决“仅查找一个事务的规范的问题”

一年内有1笔交易的账户是指有一些交易的账户减去至少有2笔交易的账户。前者正在进行交易。后者是t1.ACCTID中的那些,其中TransactionT1.tid。。。和交易t2.tid。。。t1.tid t2.tid和t1.acctid=t2.acctid和t1.transdate=t2.transdate。事务t1中的Ie在t1.tid t2.tid和t1.acctid=t2.acctid上连接事务t2,其中t1.transdate=t2.transdate


除了MySQL中没有SQL,但除此之外,您还可以使用左连接或not exists。

请参考您所指的关系代数。甚至关系也不同,更不用说操作员了。我用SQL回答了您的问题,但如果您传达了关系和运算符的相关概念,我可以给出RA版本。
select a.acctid, c.custname, c.custid
from Transactions t
and t.acctid not in (
    select t1.acctid
    from Transactions t1
    join Transactions t2
    on t1.tid <> t2.tid
    and t1.acctid = t2.acctid and t1.transdate = t2.transdate)
join Account a on t.acctid = a.acctid
join Customer c on a.custid = c.custid
where a.atype = 'savings'
and t.transdate = 2017