是否可以将一个表中的SELECT与另一个表中的另一个SELECT连接起来?

时间:2017-11-06 22:08:41

标签: sql sql-server database select

我有一张名为" Studies"它有以下列:

computed: {
  showImage: function() {
    return this.image && typeof this.image === 'object';
  },
  processedSrc: function () {
    if (this.showImage) {
      return this.image.sizes[this.size].file;
    }
  }
}

我有一张名为"学生"以下栏目和信息:

+-------------+---------------+-------------+------------+-------------+
| CodeStudent |   Type Study  | Title Study | Date Study | Place Study |
+-------------+---------------+-------------+------------+-------------+
|  10         |   Technical   |   TitleOne  | 01-02-2005 |    Narnia   |
+-------------+---------------+-------------+------------+-------------+
|  10         | Technological |   TitleTwo  | 01-05-2009 |    Mars     |
+-------------+---------------+-------------+------------+-------------+
|  10         |   University  |  TitleThree | 01-08-2012 | Gotham City |
+-------------+---------------+-------------+------------+-------------+
|  20         |   Technical   |  OtherTitle | 01-06-2011 |    Namek    |
+-------------+---------------+-------------+------------+-------------+

应考虑以下事项:

  • "学生"的CodeStudent专栏table是" Studies"的主键和CodeStudent列。 table是其各自的外键
  • 现有的唯一类型的研究是技术,技术和大学
  • 有必要说,并非所有学生都有三种类型的学习(技术,技术和大学),其中一些可能有两项研究,一项研究(如Son Goku学生的情况)或没有。

我是否需要知道是否可以在一行中显示所有信息?

如果要求学生提供代码为10的信息,则应显示:

+-------------+---------------+-------------+------------+
| CodeStudent |      Name     |   LastName  |  BirthDate |  
+-------------+---------------+-------------+------------+
|  10         |      Hug      |   Lobezno   | 02-02-2002 |
+-------------+---------------+-------------+------------+
|  20         |      Son      |    Gokú     | 05-06-2007 |
+-------------+---------------+-------------+------------+

如果要求学生提供代码为20的信息,则应显示:

+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
| CodeStudent |      Name     |   LastName  |  BirthDate |  TitleTechnical | DateTechnical | PlaceTechnical | TitleTechnological | DateTechnological | PlaceTechnological | TitleUniversity | DateUniversity | PlaceUniversity |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
|  10         |      Hug      |   Lobezno   | 02-02-2002 |    TitleOne     |   01-02-2005  |     Narnia     |      TitleTwo      |    01-05-2009     |        Mars        |    TitleThree   |   01-08-2012   |    Gotham City  |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+

我知道有一种方法就是这样:

+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
| CodeStudent |      Name     |   LastName  |  BirthDate |  TitleTechnical | DateTechnical | PlaceTechnical | TitleTechnological | DateTechnological | PlaceTechnological | TitleUniversity | DateUniversity | PlaceUniversity |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+
|  20         |      Son      |   Goku      | 05-06-2007 |    OtherTitle   |   01-06-2011  |     Namek      |       NULL         |       NULL        |        NULL        |       NULL      |      NULL      |       NULL      |
+-------------+---------------+-------------+------------+-----------------+---------------+----------------+--------------------+-------------------+--------------------+-----------------+----------------+-----------------+

但是有更好和最佳的方法吗? :P

1 个答案:

答案 0 :(得分:3)

使用条件聚合来旋转每种类型的研究的列集:

select
    S.CodeStudent
  , S.Name
  , S.LastName
  , S.BirthDate
  , TitleTechnical     = max(case when t.TypeStudy = 'Technical' then t.TitleStudy end)
  , DateTechnical      = max(case when t.TypeStudy = 'Technical' then t.DateStudy end)
  , PlaceTechnical     = max(case when t.TypeStudy = 'Technical' then t.PlaceStudy end)
  , TitleTechnological = max(case when t.TypeStudy = 'Technological' then t.TitleStudy end)
  , DateTechnological  = max(case when t.TypeStudy = 'Technological' then t.DateStudy end)
  , PlaceTechnological = max(case when t.TypeStudy = 'Technological' then t.PlaceStudy end)
  , TitleUniversity    = max(case when t.TypeStudy = 'University' then t.TitleStudy end)
  , DateUniversity     = max(case when t.TypeStudy = 'University' then t.DateStudy end)
  , PlaceUniversity    = max(case when t.TypeStudy = 'University' then t.PlaceStudy end)
from Student S
  left join Studies T  -- left join to support querying students with no studies
    on s.CodeStudent = t.CodeStudent
where S.CodeStudent = 10
group by 
    S.CodeStudent
  , S.Name
  , S.LastName
  , S.BirthDate

rextester演示:http://rextester.com/SJONU26439