【SQL解惑】谜题14:电话

本文详细介绍了如何使用SQL创建表和视图,并通过具体示例展示了表间连接及数据查询的方法。文章还提供了创建表、插入数据、构建视图、进行复杂查询等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解惑一:
1、创建表和插入数据
create table Personnel
( emp_id integer primary key ,
first_name char ( 20 ) not null,
last_name char ( 20 ) not null)

create table Phones
( emp_id integer not null,
phone_type char ( 3 ) not null
            check ( phone_type in ( 'hom' , 'fax' )),
phone_nbr char ( 12 ) not null,
primary key ( emp_id , phone_type ),
foreign key ( emp_id ) references Personnel ( emp_id ))

insert into Personnel ( emp_id , first_name , last_name )
values ( 1 , 'La' , 'Done' ),
( 2 , 'Guo' , 'Yin' ),
( 3 , 'Tim' , 'Gour' )
insert into Phones ( emp_id , phone_type , phone_nbr )
values ( 1 , 'hom' , '8845123' ),
( 2 , 'hom' , '8845123' ),
( 3 , 'hom' , '8845123' )
go
2、创建视图
create view FaxPhones ( last_name , first_name , emp_id , fax_phone )
as select e1 . last_name , e1 . first_name , e1 . emp_id , f1 . phone_nbr
       from ( Personnel as e1
               left outer join
               Phones as f1
               on e1 . emp_id = f1 . emp_id
               and f1 . phone_type = 'fax' )
go     
create view HomePhones ( last_name , first_name , emp_id , home_phone )
as select e1 . last_name , e1 . first_name , e1 . emp_id , f1 . phone_nbr
       from ( Personnel as e1
               left outer join
               Phones as f1
               on e1 . emp_id = f1 . emp_id
               and f1 . phone_type = 'hom' )    
go
select h1 . last_name , h1 . first_name , home_phone , fax_phone
from HomePhones as h1 , FaxPhones as f1
where h1 . emp_id = f1 . emp_id
go

解惑二:
1、from子句中,从Personel表中,左外连接Phones表,匹配emp_id和phone_type标识
select e1 . last_name , e1 . first_name ,
         h1 . phone_nbr as Home ,
         f1 . phone_nbr as Fax
from ( Personnel as e1
        left outer join
        Phones as h1
        on e1 . emp_id = h1 . emp_id
             and h1 . phone_type = 'hom' )
        left outer join
        Phones as f1
        on e1 . emp_id = f1 . emp_id
             and f1 . phone_type = 'fax'

解惑三:
select e1 . emp_id , e1 . first_name , e1 . last_name ,
         max ( case when p1 . phone_type = 'hom'
                         then p1 . phone_nbr
                         else null end ) as home_phone ,
         max ( case when p1 . phone_type = 'fax'
                         then p1 . phone_nbr
                         else null end ) as fax_phone
from Personnel as e1
left outer join
Phones as p1
on p1 . emp_id = e1 . emp_id
group by e1 . emp_id , e1 . first_name , e1 . last_name

解惑四:
select p1 . last_name , p1 . first_name ,
         ( select t1 . phone_nbr
              from Phones as t1
             where t1 . emp_id = p1 . emp_id
               and t1 . phone_type = 'hom' ) as home_phone ,
         ( select t2 . phone_nbr
              from Phones as t2
             where t2 . emp_id = p1 . emp_id
               and t2 . phone_type = 'fax' ) as fax_phone
from Personnel as p1




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值