【问题标题】:How to extract data from two tables using SQL object relational statement in oracle 11goracle 11g中如何使用SQL对象关系语句从两个表中提取数据
【发布时间】:2018-03-12 10:47:45
【问题描述】:

我正在尝试使用对象关系方法解决以下查询,但不知道什么是正确的方法。

查找每个分行的储蓄账户数量,显示 号码和分行地址。

我创建了两个表并插入一些数据,如下所示:

--这是分支表:

create type Branch_Address as object(
street varchar2(20),
city varchar2(20),
p_code varchar2(10))
not final
/

create type Branch_Phone as object(
phone varchar2(20))
not final;
/

create type branch_type as object(
bID varchar2(10),
bAddress Branch_Address,
bPhone Branch_Phone)
/

create table branch of branch_type(
primary key (bID))
/

insert into branch values(
'901',Branch_Address('Nic Street','Jordan','ABH887A'),Branch_Phone('0335454888'));
/
insert into branch values(
'906',Branch_Address('East End Garden','California','L181QP'),Branch_Phone('07455668711'));
/
insert into branch values(
'912',Branch_Address('Fredrick Street','London','LA112AS'),Branch_Phone('02841124478'));
/

insert into branch values(
'924',Branch_Address('West Street','Cambridge','CA8L871'),Branch_Phone('04511477885'));

--这是账户表

create type account_type as object(
accNum int,
accType varchar2(15),
balance number, 
bID ref branch_type,
inRate number,
limitOfFreeOD number,
openDate DATE)
/
create table account_table of account_type(
primary key (accNum))
/

insert into account_table
select account_type('1001','current','820.50',ref(b),'0.005','800','01-May-11')
from branch b
where b.bID = '901';
/

insert into account_table 
select account_type('1010','saving','2155',ref(b),'0.02','0','08-Mar-10')
from branch b
where b.BID = '906';
/
insert into account_table 
select account_type('1002','current','2600',ref(b),'0.005','1000','10-Apr-13')
from branch b
where b.BID = '912';
/
insert into account_table 
select account_type('1112','saving','24000',ref(b),'0','1700','16-Jun-16')
from branch b
where b.BID = '924';
/

分支(bID、街道、城市、p_code、bPhone)

Account(accNum, accType, balance, bID, inRate, limitOfFreeOD, openDate)

粗体是主键 斜体是外键(在对象关系中,如果我是对的,我们不使用 Join)。

有什么帮助吗?谢谢。

【问题讨论】:

    标签: sql oracle oracle11g


    【解决方案1】:

    您可以通过以下方式加入:

    select b.bid, 
        (select count(1) from account_table a 
          where a.bid.bid = b.bid
          and a.acctype='saving'
          ) as num_accounts,
        b.baddress.street, b.baddress.city, b.baddress.p_code 
    from branch b;
    

    这更令人困惑,因为branch_type.bIDvarchar2(10)account_type.bIDref branch_type。我会将 account_type 上的 bID 重命名为 accBranch 之类的名称,以便更清楚地表明它不存储 ID,而是对整个对象的引用。

    我没有测试过这个查询,但是你也可以把它写成一个连接(下)而不是一个子查询(上)。它们各有用途。

    select b.bid, count(1) as num_accounts,
        b.baddress.street, b.baddress.city, b.baddress.p_code 
    from branch b
    join account_table a
      on a.bid.bid = b.bid
      and a.acctype='saving'
    group by b.bid, b.baddress.street, b.baddress.city, b.baddress.p_code;
    

    【讨论】:

    • 只提取储蓄账户,我应该在最后一行之后使用“ where a.acctype='saving' ”吗?感谢您的大力帮助和解释。
    • 我编辑在 accType 上包含一个过滤器,还添加了第二个版本作为连接而不是子查询。如果它不起作用,请 lmk。
    • 谢谢。它就像魔术一样工作。只是说,你能帮我多问几个问题吗?
    • 如果您有其他问题需要帮助,您应该将它们作为单独的问题发布。我不是专家,但我会尽力提供帮助。 :)
    • 我相信你可以帮助我。我可以通过电子邮件发送给您吗?
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2012-08-23
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多