对包含异常数据的表的复杂SQL查询要求

问题描述 投票:1回答:1

我从表中提取信息有点复杂。我不确定什么是最好的方法。

我正在列出表格示例:

Student Table

Student Information

Joining both

I need information just for Dept and Class for each student

Required Fields

Can we construct the table like this?

Desired OUTPUT

mysql sql rdbms
1个回答
1
投票

您可以加入两个表,然后按学生进行汇总,使用枢轴逻辑得出所需的属性:

SELECT
    s.id,
    s.name,
    MAX(CASE WHEN si.property = 'dept'  THEN si.value END) AS dept,
    MAX(CASE WHEN si.property = 'class' THEN si.value END) AS class
FROM student s
LEFT JOIN student_information si
    ON s.id = si.student_id
GROUP BY
    s.id,
    s.name;

screen capture from demo link below

Demo

© www.soinside.com 2019 - 2024. All rights reserved.