交叉資料表查詢sql語句

來源:互聯網
上載者:User

表一:   
  組名   
  成員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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.