image.png

    1. -- 语文
    2. CREATE VIEW v_chinese AS SELECT sid, score AS chinese
    3. FROM (
    4. SELECT *,
    5. if(subject = 'chinese', 1, 0) AS is_c
    6. FROM test
    7. ) AS chinese_score
    8. WHERE is_c = 1;
    9. -- 数学
    10. CREATE VIEW v_math AS SELECT sid, score AS math
    11. FROM (
    12. SELECT *,
    13. if(subject = 'math', 1, 0) AS is_m
    14. FROM test
    15. ) AS math_score
    16. WHERE is_m = 1;
    17. -- 英语
    18. CREATE VIEW v_english AS SELECT sid, score AS english
    19. FROM (
    20. SELECT *,
    21. if(subject = 'english', 1, 0) AS is_e
    22. FROM test
    23. ) AS eng_score
    24. WHERE is_e = 1;
    25. -- 连接
    26. SELECT
    27. c.sid,
    28. chinese,
    29. math,
    30. english
    31. FROM v_chinese AS c
    32. INNER JOIN v_math AS m ON c.sid = m.sid
    33. INNER JOIN v_english AS e ON c.sid = e.sid;