SQL server 视图的一个小问题
小弟的数据库有这么两张表 员工(id,name) 入库单(id,操作员,业务员)
入库单的操作员和业务员字段是相应的员工ID,想在我想建个视图,把相应的业务员操作员ID显示为姓名,这个SQL语句应该怎么写呢(为了说明问题简化了表),
[解决办法]
SELECT 入库单.id, ta.name, tb.name FROM 入库单
JOIN
员工 AS ta WHERE 入库单.操作员 = ta.id
JOIN
员工 AS tb WHERE 入库单.业务员 = tb.id
SELECT 入库单.id, ta.name, tb.name FROM 入库单
JOIN
员工 AS ta ON 入库单.操作员 = ta.id
JOIN
员工 AS tb ON 入库单.业务员 = tb.id
create view [视图名]
as
select a.id,
b.name '操作员',
c.name '业务员'
from 入库单 a
left join 员工 b on a.操作员=b.id
left join 员工 c on a.业务员=c.id