sql - 显示有账户的客户名称 SQL Query Oracle 10G
问题描述
create table customer
(cust_id integer not null,
cust_name char(20) not null ,
cust_address varchar2(200) ,
emp_id integer not null,
constraint pk_customer primary key(cust_id)
);
create table account
(account_number integer not null,
account_balance number(8,2) not null,
constraint pk_acount primary key(account_number)
);
create table has
(cust_id integer not null,
account_number integer not null,
constraint pk_has
primary key(cust_id, account_number) )
alter table has
add constraint fk_account_has foreign key(account_number)
references account(account_number);
alter table has
add constraint fk_customer_has foreign key(cust_id)
references customer(cust_id);
Q1 显示有账户的客户名称
Q2 显示客户姓名和他们打交道的员工姓名**
解决方案
Q1 是对cust_id
in junction 表的简单查找has
:
select c.cust_name
from customer c
where exists (select 1 from has h where h.cust_id = c.cust_id)
这句话为:选择表中至少有一个条目的客户has
。
关于 Q2:您的数据结构没有显示员工的迹象(我们只有客户和帐户),因此无法根据您提供的信息来回答。您可能想为此提出一个新问题,为所涉及的表提供示例数据,以及所需的结果以及您当前解决问题的尝试。
推荐阅读
- davinci - 从 arecord 到 mcasp 编解码器的旅程
- mysql - MySQL插入使用功能不起作用
- android - 无法选择是免费还是付费安卓应用
- shell - awk 多个字段中的多个字符串
- vba - VBA 将交替列复制到相邻列
- python - 当 groupby 另一个时,pandas 在组中至少获得一列
- angular - FAB 在 Ionic 和事件中的选项卡栏上
- tensorflow - tf.train.batch 中的错误 - FIFOQueue 已关闭且元素不足
- javascript - 在谷歌地图上放一些不同颜色的点
- ruby-on-rails - 单页多语言 - Ruby on Rails