我有一张名为" 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 |
+-------------+---------------+-------------+------------+-------------+
应考虑以下事项:
我是否需要知道是否可以在一行中显示所有信息?
如果要求学生提供代码为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
答案 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