兴国资源网 Design By www.nnzcdc.com
表一:
组名
成员1id
成员2id
成员3id
示例数据:
冲锋组 1 2 3
后卫组 2 3 4
表二:
成员id
成员姓名
示例数据:
1 张三
2 李四
3 王五
4 陆二
要求结果
冲锋组 张三 李四 王五
后卫组 李四 王五 陆二
复制代码 代码如下:
--建立测试环境
Create Table 表1(组名 varchar(10),成员1id varchar(10),成员2id varchar(10),成员3id varchar(10))
--插入数据
insert into 表1
select '冲锋组','1','2','3' union
select '后卫组','2','3','4'
Create Table 表2(成员id varchar(10),成员姓名 varchar(10))
--插入数据
insert into 表2
select '1','张三' union
select '2','李四' union
select '3','王五' union
select '4','陆二'
--测试语句
select a.组名,
成员1=(select 成员姓名 from 表2 b where a.成员1id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员2id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员3id=b.成员id)
from 表1 a
--删除测试环境
Drop Table 表1
Drop Table 表2
/*
组名 成员1 成员1 成员1
---------- ---------- ---------- ----------
冲锋组 张三 李四 王五
后卫组 李四 王五 陆二
(所影响的行数为 2 行)
*/
复制代码 代码如下:
select
a.组名,
成员1 = max(case b.成员id = a.成员1id then b.成员姓名 end),
成员2 = max(case b.成员id = a.成员2id then b.成员姓名 end),
成员3 = max(case b.成员id = a.成员3id then b.成员姓名 end),
from
表一 a,
表二 b
group by
a.组名
复制代码 代码如下:
select
a.组名,
成员1 = max(case b.成员id = a.成员1id then b.成员姓名 end),
成员2 = max(case b.成员id = a.成员2id then b.成员姓名 end),
成员3 = max(case b.成员id = a.成员3id then b.成员姓名 end)
from
表一 a,
表二 b
group by
a.组名
复制代码 代码如下:
select a.组名,
成员1=(select 成员姓名 from 表2 b where a.成员1id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员2id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员3id=b.成员id)
from 表一 a
复制代码 代码如下:
正解是
select 表1.组名,
(select 表1.成员姓名 from 表2 b where 表1.成员1id=表2.成员id) as 成员1id,
(select 表1.成员姓名 from 表2 b where 表1.成员2id=表2.成员id) as 成员2id,
(select 表1.成员姓名 from 表2 b where 表1.成员3id=表2.成员id) as 成员3id
from 表1,表2
组名
成员1id
成员2id
成员3id
示例数据:
冲锋组 1 2 3
后卫组 2 3 4
表二:
成员id
成员姓名
示例数据:
1 张三
2 李四
3 王五
4 陆二
要求结果
冲锋组 张三 李四 王五
后卫组 李四 王五 陆二
复制代码 代码如下:
--建立测试环境
Create Table 表1(组名 varchar(10),成员1id varchar(10),成员2id varchar(10),成员3id varchar(10))
--插入数据
insert into 表1
select '冲锋组','1','2','3' union
select '后卫组','2','3','4'
Create Table 表2(成员id varchar(10),成员姓名 varchar(10))
--插入数据
insert into 表2
select '1','张三' union
select '2','李四' union
select '3','王五' union
select '4','陆二'
--测试语句
select a.组名,
成员1=(select 成员姓名 from 表2 b where a.成员1id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员2id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员3id=b.成员id)
from 表1 a
--删除测试环境
Drop Table 表1
Drop Table 表2
/*
组名 成员1 成员1 成员1
---------- ---------- ---------- ----------
冲锋组 张三 李四 王五
后卫组 李四 王五 陆二
(所影响的行数为 2 行)
*/
复制代码 代码如下:
select
a.组名,
成员1 = max(case b.成员id = a.成员1id then b.成员姓名 end),
成员2 = max(case b.成员id = a.成员2id then b.成员姓名 end),
成员3 = max(case b.成员id = a.成员3id then b.成员姓名 end),
from
表一 a,
表二 b
group by
a.组名
复制代码 代码如下:
select
a.组名,
成员1 = max(case b.成员id = a.成员1id then b.成员姓名 end),
成员2 = max(case b.成员id = a.成员2id then b.成员姓名 end),
成员3 = max(case b.成员id = a.成员3id then b.成员姓名 end)
from
表一 a,
表二 b
group by
a.组名
复制代码 代码如下:
select a.组名,
成员1=(select 成员姓名 from 表2 b where a.成员1id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员2id=b.成员id),
成员1=(select 成员姓名 from 表2 b where a.成员3id=b.成员id)
from 表一 a
复制代码 代码如下:
正解是
select 表1.组名,
(select 表1.成员姓名 from 表2 b where 表1.成员1id=表2.成员id) as 成员1id,
(select 表1.成员姓名 from 表2 b where 表1.成员2id=表2.成员id) as 成员2id,
(select 表1.成员姓名 from 表2 b where 表1.成员3id=表2.成员id) as 成员3id
from 表1,表2
兴国资源网 Design By www.nnzcdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
兴国资源网 Design By www.nnzcdc.com
暂无评论...
更新日志
2024年04月24日
2024年04月24日
- 汪峰 许巍《巍峰凛凛·实力比拼》3CD[WAV分轨][2.2G]
- dnf白字红字黄字是什么意思
- dnf石化鞋怎么搭配
- dnf神之真谛称号属性
- 《天国拯救2》游戏特色介绍
- 《无感染区》开局建家探索心得
- 《圣剑传说Visions of Mana》灵器作用介绍
- 吴克群2023-今天很OK[何乐音乐][WAV+CUE]
- 邱兰芬.2002-台湾红歌100年·台语精选辑CD19【乡城】【WAV+CUE】
- 满江.1998-多变的海【SONY】【WAV+CUE】
- 赞达亚宣传R级新片《挑战者》 粉色裙装秀出纤细蛮腰
- 《老头环》经常被玩家跳过的BOSS:多个双人组上榜
- B社辐射3开发者曾遭死亡威胁:对方扬言割断其喉咙
- 原画壁纸及美图第117期,无水印可自取
- 原画壁纸及美图第118期,无水印可自取