看CSDN测试后,关于领导职工显示的问题!!!
2006-10-09 22:28
316 查看
create table staff(staff_id int,staff_name varchar(6))---建测试表,职工表
go
insert staff
select 1,'小王' union all
select 2,'小刘' union all
select 3,'赵科长'union all
select 4,'吴经理' union all
select 5, '董主任'
go
select * ---------------显示表记录
from staff
go
create table staff_leader_relation(staff_id int ,leader_id int )----建立员工关系表
alter table staff_leader_relation add constraint pk_relation foreign key(staff_id) references staff(staff_id) -----表外建FK
go
alter table staff add constraint pk primary key nonclustered(staff_id)---------------- PK
alter table staff_leader_relation add constraint pk2_relation foreign key(leader_id) references staff(staff_id)
go
select *
from staff_leader_relation --- 此时为空,可先为两个有外键联系的表先建立外键约束,再输入记录
go
insert staff_leader_relation
select 1,3 union all
select 2,3 union all
select 3,4 union all
select 5,4
go
select *
from staff_leader_relation
----展现出-----------------------------------------------------------------
staff_name leader_name
----------------------------------------------------------------------------------
select x.staff_name,y.staff_name as leader_name ---------我作的,可显示,但是没有作业领导职工的人显示不出来
from staff x,staff_leader_relation s,staff y
where x.staff_id=s.staff_id and s.leader_id=y.staff_id
select distinct a.staff_name,c.staff_name as leader_name from staff a,staff_leader_relation b,staff c -------- 同上
where a.staff_id=b.staff_id
and b.leader_id=c.staff_id
order by a.staff_name
select ---------------splory给出的完整答案,包括职工吴经理(null)
(select staff_name from staff where staff_id=a.staff_id) xiashu
,(select staff_name from staff where staff_id=b.leader_id) lingdao
from staff a left join sl_ralation b
on a.staff_id=b.staff_id
---------------------------------------------
--------------------------------------------
select A.staff_name,B.staff_name -----------中山大学开出答案
from staff A,staff B,sl_ralation C
where A.staff_id=C.staff_id and B.staff_id=C.leader_id
union
select A.staff_name,null
from staff A
where A.staff_id not in (select staff_id from sl_ralation)
相关文章推荐
- 关于在测试php文件时在浏览器上显示源码问题
- 关于CSDN书写的博客内容中图片不显示的问题
- 关于Xcode8.1 / iOS10+ 真机测试系统打印或者宏定义打印不显示问题
- 关于测试报告以正文形式发送到邮件时,a标签不显示的问题
- 关于CSDN登录时,图片检验码无法显示的问题处理
- 关于字符latin capital letter sharp s "ß"( U+1E9E)显示的问题
- 关于QWidget的子类调用show()方法不能显示的问题
- 关于MyEclipse SVN显示资源历史记录乱码问题
- android中关于textview字数显示的问题
- 关于显示生产订单配置信息所遇到的问题
- 关于android gridview不显示图片,文字等问题
- 关于各种进制的转换与数据在编辑框中显示的若干问题
- android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结
- android 关于 ScrollView嵌套GridView,ListView 显示和滑动的问题
- 关于list集合传到jsp,在struts的select标签中显示下拉列表问题
- 关于jsp页面显示的时间格式和Oracle数据库中的格式不一样的问题
- 关于show arp表,显示Incomplete问题
- 关于手机调试不能显示Toast问题
- 测试一下动态棋谱 这个垃圾CSDN博客, 编辑的时候显示正常, 发表之后只有代码, 妈的
- 关于加载cocos2dx动画问题,跳转页面后回来不显示,报错找不到资源