当前位置: 代码迷 >> SQL >> 用sql展示如图所示的查询结果
  详细解决方案

用sql展示如图所示的查询结果

热度:39   发布时间:2016-05-05 14:23:39.0
用sql显示如图所示的查询结果
创建testtable1与testtable2表。创建语句如下

create table testtable1(id int IDENTITY,department varchar(12) )select * from testtable1insert into testtable1 values('设计')insert into testtable1 values('市场')insert into testtable1 values('售后')create table testtable2(id int IDENTITY,dptID int,name varchar(12))insert into testtable2 values(1,'张三')insert into testtable2 values(1,'李四')insert into testtable2 values(2,'王五')insert into testtable2 values(3,'彭六')insert into testtable2 values(4,'陈七')


对应的的testtable1与testtable2表的数据对应如下图所示




要求显示的效果如下图所示





我使用的sql查询语句如下:

select ta.*,ISNULL(department,'黑人') from testtable2 ta left join testtable1 tb on ta.dptid=tb.id