国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > 文庫(kù) > 正文

交叉表查詢sql語(yǔ)句

2020-10-29 21:56:05
字體:
供稿:網(wǎng)友
表一:   
  組名   
  成員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è)試語(yǔ)句   
    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
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安多县| 吉安县| 盘山县| 顺昌县| 昌平区| 滕州市| 海丰县| 阜城县| 临西县| 丘北县| 五大连池市| 临泽县| 龙陵县| 五莲县| 高平市| 新绛县| 广饶县| 临湘市| 交口县| 华容县| 正定县| 冀州市| 黄骅市| 中卫市| 惠水县| 政和县| 定安县| 屏南县| 永泰县| 德格县| 张家口市| 贺州市| 陵川县| 浦江县| 绿春县| 衡南县| 凉城县| 通州市| 固原市| 玉树县| 洪泽县|