帝王谷资源网 Design By www.wdxyy.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
标签:
交叉表查询sql语句
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年05月02日
2024年05月02日
- 群星《影视剧 群星闪耀时 影视原声带》[320K/MP3][38.69MB]
- 姜育恒.1998-成名金曲(马来西亚瑞华金碟珍藏版)【瑞华】【WAV+CUE】
- 水木年华.2006-生命狂想曲特别版2CD【水木同创】【WAV+CUE】
- 卢庚戌.2000-未来的未来【喜洋洋】【WAV+CUE】
- 钟汉良.1997-亲热【艺能动音】【WAV+CUE】
- 满文军.2002-我需要你【新索音乐】【WAV+CUE】
- 毛阿敏.2000-毛阿敏·精选集【中唱】【WAV+CUE】
- 钟明秋《爱有天意HQ》头版限量[低速原抓WAV+CUE]
- 云飞《云在飞HQ》头版限量编号[低速原抓WAV+CUE]
- 戏班-就是这个调调【FLAC分轨】
- 黄品源《寂寞旋律》[WAV+CUE][367M]
- 群星《家传户晓~我们三十年的主题曲XRCD》[WAV分轨][532M]
- 谢军《那一夜+又一夜》2CD[WAV分轨][1.2G]
- 苏有朋.1992-我只要你爱我【飞碟】【WAV+CUE】
- 谢金燕.2010-爱你辣【乾坤唱片】【WAV+CUE】