【SQL解惑】谜题14:电话
来源:互联网 发布:ubuntu 完全卸载samba 编辑:程序博客网 时间:2024/05/18 05:15
解惑一:
1、创建表和插入数据
createtable Personnel
(emp_idinteger primary key,
first_namechar(20)not null,
last_namechar(20)not null)
createtable Phones
(emp_idinteger not null,
phone_typechar(3)not null
check(phone_typein ('hom','fax')),
phone_nbrchar(12)not null,
primarykey (emp_id,phone_type),
foreignkey (emp_id)references Personnel(emp_id))
insertinto Personnel(emp_id,first_name,last_name)
values(1,'La','Done'),
(2,'Guo','Yin'),
(3,'Tim','Gour')
insertinto Phones(emp_id,phone_type,phone_nbr)
values(1,'hom','8845123'),
(2,'hom','8845123'),
(3,'hom','8845123')
go
2、创建视图
createview FaxPhones (last_name,first_name,emp_id,fax_phone)
asselect e1.last_name,e1.first_name,e1.emp_id,f1.phone_nbr
from (Personnelas e1
left outer join
Phones as f1
on e1.emp_id= f1.emp_id
and f1.phone_type= 'fax')
go
createview HomePhones (last_name,first_name,emp_id,home_phone)
asselect e1.last_name,e1.first_name,e1.emp_id,f1.phone_nbr
from (Personnelas e1
left outer join
Phones as f1
on e1.emp_id= f1.emp_id
and f1.phone_type= 'hom')
go
selecth1.last_name,h1.first_name,home_phone,fax_phone
fromHomePhones as h1,FaxPhonesas f1
whereh1.emp_id= f1.emp_id
go
解惑二:
1、from子句中,从Personel表中,左外连接Phones表,匹配emp_id和phone_type标识
selecte1.last_name,e1.first_name,
h1.phone_nbras Home,
f1.phone_nbras Fax
from(Personnelas e1
left outer join
Phones as h1
on e1.emp_id= h1.emp_id
andh1.phone_type= 'hom')
left outer join
Phones as f1
on e1.emp_id= f1.emp_id
andf1.phone_type= 'fax'
解惑三:
selecte1.emp_id,e1.first_name,e1.last_name,
max(casewhen p1.phone_type= 'hom'
thenp1.phone_nbr
elsenull end)as home_phone,
max(casewhen p1.phone_type= 'fax'
thenp1.phone_nbr
elsenull end)as fax_phone
fromPersonnel as e1
leftouter join
Phonesas p1
onp1.emp_id= e1.emp_id
groupby e1.emp_id,e1.first_name,e1.last_name
解惑四:
selectp1.last_name,p1.first_name,
(selectt1.phone_nbr
from Phones as t1
where t1.emp_id= p1.emp_id
and t1.phone_type= 'hom')as home_phone,
(selectt2.phone_nbr
from Phones as t2
where t2.emp_id= p1.emp_id
and t2.phone_type= 'fax')as fax_phone
fromPersonnel as p1
阅读全文
0 0
- 【SQL解惑】谜题14:电话
- 【SQL解惑】谜题13:教师
- SQL解惑
- sql解惑2 谜题53 之lag
- SQL解惑-谜题32计算税收
- 【SQL解惑】谜题3:麻醉师谜题
- 【SQL解惑】谜题4:门禁卡
- 【SQL解惑】谜题7:跟踪投资组合
- 【SQL解惑】谜题8:调度打印机
- 【SQL解惑】谜题9:空座位
- 【SQL解惑】谜题11:工作顺序
- 【SQL解惑】谜题12:索赔状态
- 【SQL解惑】谜题18:广告信件
- 【SQL解惑】谜题19:销售冠军
- 【SQL解惑】谜题20:测验结果
- 【SQL解惑】谜题21:飞机与飞行员
- 学习《SQL解惑》
- sql解惑之里程碑 谜题25(行转列)
- 题解。。。。
- 无穷小微积分挺近微信平台,进入普及快车道
- linux上实现邮箱系统
- 【SQL解惑】谜题13:教师
- python blinker 信号库学习
- 【SQL解惑】谜题14:电话
- 指针 const关键字
- Java 之 文件读写及性能比较总结
- PAT
- PAT乙级 1071 小赌怡情
- jlable setsize和setbackground无法设置背景和大小
- windows10 update 下载的更新保存在C盘的位置
- 读书笔记《C++ Primer》第五版——第十五章 面向对象程序设计
- Test.java 判断是否为空,判断是否相等util工具