首页 > 解决方案 > 显示有账户的客户名称 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 显示客户姓名和他们打交道的员工姓名**

标签: sqldatabaseoracleoracle10gqsqlquery

解决方案


Q1 是对cust_idin 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:您的数据结构没有显示员工的迹象(我们只有客户和帐户),因此无法根据您提供的信息来回答。您可能想为此提出一个问题,为所涉及的表提供示例数据,以及所需的结果以及您当前解决问题的尝试。


推荐阅读