如何选择这样的2表

时间:2016-12-06 19:47:27

标签: mysql sql sql-server

如何选择具有这种条件的表格。

我有一张桌子可以容纳各种各样的科目。

table subjects

|===========|============|
|Subjects id|  Subjects  |
|===========|============|
|     01    |mathematics |      
|     02    |biology     |     
|     03    |geography   |       
|     04    |physics     |      
|===========|============|

然后我还有一张表来保存每个学生的价值。

table score

|==========|============|===============|
|Student id|Subjects id |     Score     |
|==========|============|===============|
|  10001   |     01     |       8       |
|  10001   |     02     |       6       |
|  10001   |     03     |       7       |
|  10001   |     04     |       9       |
|  10002   |     01     |       5       |
|  10002   |     02     |       7       |
|  10002   |     03     |       10      |
|  10002   |     04     |       7       |
|  10003   |     01     |       6       |
|  10003   |     02     |       7       |
|  10003   |     03     |       8       |
|  10003   |     04     |       9       |
|==========|============|===============|

我想用类似下表的表单创建一个查询,但我不知道如何制作它。

|==========|=============|=========|===========|=========|
|Student id| mathematics | biology | geography | physics |
|==========|=============|=========|===========|=========|
|  10001   |     8       |    6    |     7     |    9    |
|  10002   |     5       |    7    |    10     |    7    |
|  10003   |     6       |    7    |     8     |    9    |
|==========|=============|=========|===========|=========|

请帮我解决这个问题。对不起,我的英语不好。我还是初学者

4 个答案:

答案 0 :(得分:1)

这应该有效,假设得分表中没有重复,这些是你拥有的唯一4个科目。或者你也可以使用PIVOT。

SELECT
    sub.[Student Id]
    , CASE sc.[Subjects ID]
        WHEN '01' THEN sc.Score
        ELSE NULL
    AS mathematics
    , CASE sc.[Subjects ID]
        WHEN '02' THEN sc.Score
        ELSE NULL
    AS biology
    , CASE sc.[Subjects ID]
        WHEN '03' THEN sc.Score
        ELSE NULL
    AS geography
    , CASE sc.[Subjects ID]
        WHEN '04' THEN sc.Score
        ELSE NULL
    AS physics
FROM
    subjects sub 
    JOIN score sc ON sub.[Subjects ID] = sc.[Subjects ID]

答案 1 :(得分:1)

你有办法做到这一点,但是尽量不创建临时表,你可以这样做:

select
    s.id,
    avg(case when sb.id = '01' then s.score end) as math,
    avg(case when sb.id = '02' then s.score end) as bio

from student s
join subject sb on (sb.id = s.subject_id)

group by s.id

根据需要填写总和/案例行到其他科目!

希望它有所帮助。

答案 2 :(得分:0)

您可以像其他答案一样使用组,或者像这样使用左连接:

select
  students.id, 
  mat.score as mathematics,
  bio.score as biology,
  geo.score as geography,
  phy.score as physics
from (
  SELECT DISTINCT studentid as id
  from score
) as students
left join score as mat on mat.studentid = students.id and mat.subjectid = 1
left join score as bio on bio.studentid = students.id and bio.subjectid = 2
left join score as geo on geo.studentid = students.id and geo.subjectid = 3
left join score as phy on phy.studentid = students.id and phy.subjectid = 4

答案 3 :(得分:0)

您可以使用:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Subjects) 
                    from subjects
                    group by Subjects
                    order by Subjects
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Student_id,' + @cols + ' 
            from 
             (
                select      D2.Student_id, D1.Subjects, D2.Score
                From        subjects D1
                Inner Join  score D2 
                    On      D1.Subjects_id = D2.Subjects_id
            ) x
            pivot 
            (
                sum(Score)
                for Subjects in (' + @cols + ')
            ) p '

execute(@query);