交叉表查詢sql語句
更新時(shí)間:2007年06月29日 00:00:00 作者:
表一:
組名
成員1id
成員2id
成員3id
示例數(shù)據(jù):
沖鋒組 1 2 3
后衛(wèi)組 2 3 4
表二:
成員id
成員姓名
示例數(shù)據(jù):
1 張三
2 李四
3 王五
4 陸二
要求結(jié)果
沖鋒組 張三 李四 王五
后衛(wèi)組 李四 王五 陸二
--建立測(cè)試環(huán)境
Create Table 表1(組名 varchar(10),成員1id varchar(10),成員2id varchar(10),成員3id varchar(10))
--插入數(shù)據(jù)
insert into 表1
select '沖鋒組','1','2','3' union
select '后衛(wèi)組','2','3','4'
Create Table 表2(成員id varchar(10),成員姓名 varchar(10))
--插入數(shù)據(jù)
insert into 表2
select '1','張三' union
select '2','李四' union
select '3','王五' union
select '4','陸二'
--測(cè)試語句
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
--刪除測(cè)試環(huán)境
Drop Table 表1
Drop Table 表2
/*
組名 成員1 成員1 成員1
---------- ---------- ---------- ----------
沖鋒組 張三 李四 王五
后衛(wèi)組 李四 王五 陸二
(所影響的行數(shù)為 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
示例數(shù)據(jù):
沖鋒組 1 2 3
后衛(wèi)組 2 3 4
表二:
成員id
成員姓名
示例數(shù)據(jù):
1 張三
2 李四
3 王五
4 陸二
要求結(jié)果
沖鋒組 張三 李四 王五
后衛(wèi)組 李四 王五 陸二
復(fù)制代碼 代碼如下:
--建立測(cè)試環(huán)境
Create Table 表1(組名 varchar(10),成員1id varchar(10),成員2id varchar(10),成員3id varchar(10))
--插入數(shù)據(jù)
insert into 表1
select '沖鋒組','1','2','3' union
select '后衛(wèi)組','2','3','4'
Create Table 表2(成員id varchar(10),成員姓名 varchar(10))
--插入數(shù)據(jù)
insert into 表2
select '1','張三' union
select '2','李四' union
select '3','王五' union
select '4','陸二'
--測(cè)試語句
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
--刪除測(cè)試環(huán)境
Drop Table 表1
Drop Table 表2
/*
組名 成員1 成員1 成員1
---------- ---------- ---------- ----------
沖鋒組 張三 李四 王五
后衛(wèi)組 李四 王五 陸二
(所影響的行數(shù)為 2 行)
*/
復(fù)制代碼 代碼如下:
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.組名
復(fù)制代碼 代碼如下:
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.組名
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
正解是
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
相關(guān)文章
MySQL與Redis如何保證數(shù)據(jù)一致性詳解
在高并發(fā)的業(yè)務(wù)場景下數(shù)據(jù)庫大多數(shù)情況都是用戶并發(fā)訪問最薄弱的環(huán)節(jié),所以就需要使用redis做一個(gè)緩沖操作,讓請(qǐng)求先訪問到redis,而不直接訪問Mysql等數(shù)據(jù)庫,這篇文章主要給大家介紹了關(guān)于MySQL與Redis如何保證數(shù)據(jù)一致性的相關(guān)資料,需要的朋友可以參考下2021-08-08DBeaver一款替代Navicat的數(shù)據(jù)庫可視化工具
這篇文章主要介紹了DBeaver一款替代Navicat的數(shù)據(jù)庫可視化工具,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11idea中連接數(shù)據(jù)庫時(shí)出現(xiàn)SSL錯(cuò)誤的問題
這篇文章主要介紹了idea中連接數(shù)據(jù)庫是出現(xiàn)SSL錯(cuò)誤的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10dbeaver工具連接達(dá)夢(mèng)數(shù)據(jù)庫的完整步驟
DBeaver數(shù)據(jù)庫連接工具是我用了這么久最好用的一個(gè)數(shù)據(jù)庫連接工具,擁有的優(yōu)點(diǎn),支持的數(shù)據(jù)庫多、快捷鍵很贊、導(dǎo)入導(dǎo)出數(shù)據(jù)非常方便,下面這篇文章主要給大家介紹了關(guān)于dbeaver工具連接達(dá)夢(mèng)數(shù)據(jù)庫的完整步驟,需要的朋友可以參考下2023-05-05Linux下mysql數(shù)據(jù)庫的創(chuàng)建導(dǎo)入導(dǎo)出 及一些基本指令
這篇文章主要介紹了Linux數(shù)據(jù)庫的創(chuàng)建 導(dǎo)入導(dǎo)出 以及一些基本指令,需要的朋友可以參考下2019-08-08