DDR爱好者之家 Design By 杰米
表一:
组名
成员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
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年04月26日
2024年04月26日
- 群星《发烧混声金唱盘》3CD[MP3][481M]
- DominikWagner-DoubleBassRhapsody(2023)[24-48]FLAC
- 和平之月系列PMR-0048贾鹏芳《浪漫Roman》[WAV+CUE]
- 和平之月系列PMR-0063贾鹏芳二胡《月光Moonlight》[WAV+CUE]
- 黄乙玲.1992-悲恋梦【歌林】【WAV+CUE】
- 许巍.2001-我只有两天精丫红星生产社】【WAV+CUE】
- 王芷蕾.2001-盛开精选2辑【华纳】【WAV+CUE】
- 群星.2003-LOVE情歌集【正东】【WAV+CUE】
- 张其萍《美丽中国梦》[WAV+CUE]
- 群星《嗨麦顶级中文舞曲·金牌舞曲精逊2CD[WAV分轨]
- 群星《发烧女生 DSD》[WAV+CUE][625M]
- 张学友《私人珍藏(黑胶CD)》2CD[WAV+CUE][1.2G]
- 刘紫玲《紫音幽韵(DSD)》[WAV+CUE][441M]
- 刘珺儿.2004-靓人靓歌5CD【音乐谷】【WAV+CUE】
- 康乔.1991-精丫丽风】【WAV+CUE】