当前位置: 代码迷 >> Sql Server >> SQL视图创建解决办法
  详细解决方案

SQL视图创建解决办法

热度:98   发布时间:2016-04-27 12:29:38.0
SQL视图创建
比如我有两个表
一个employee 一个department
employee表中有employeeid,name,email,deptid
department表中有deptid,deptname,daptname,managerid

我现在创建这两个表的视图,要求视图中包含
employeeid,name,email,managername,manageremail
前三个能直接勾选,后两个该怎么做啊,只知道通过employee表与department表结合能得到managername和managermail
但是怎么放在视图中呢?

求解!!第一次做视图....

------解决方案--------------------
select 查找字段 from employee e inner join department d
on e.deptid=p.deptid ,email,managername,manageremail这些在哪个表呀,就是关联查询
------解决方案--------------------
就是两表关联查询 通过deptid关联


你把第一个表拖进到视图当中 可以勾选 ,第二个表的字段 当然也可以勾选啊 主要还是通过deptid关联
------解决方案--------------------
SQL code
如果department.managerid来自表employee.employeeid的话create view view_employeeas select a.employeeid,a.name,a.email,b.name as managername ,b.email as manageremailfrom employee a left join department d on a.deptid = d.deptidleft join employee b on d.managerid = b.employeeid
------解决方案--------------------
SQL code
CREATE VIEW dbo.view_employee_departmentASselect employeeid,name,emailfrom employeeUNIONselect name as managename,email as  manegeemail from employee a left join department b on a.deptid=b.deptid
------解决方案--------------------
探讨
引用:

SQL code
CREATE VIEW dbo.view_employee_department
AS
select employeeid,name,email
from employee
UNION
select name as managename,email as manegeemail
from employee a left join dep……
  相关解决方案