如何 select 并加入具有行名的多个表

How to select and join into multiple tables with row names

结算table

 | mainid   | subID   | subid_name  | subid_value  |
 |----------|---------|-------------|--------------|
 | 100      | 3478    | name        | Ali Baba     |
 | 100      | 2373    | school_type | ghetto       |
 | 100      | 2989    | school_loc  | 41000        |
 | 100      | 9824    | fee_sem     | 40           |
 | 100      | 283     | desc        | thieves      |
 | 100      | 32383   | CGPA_grade  | 2.9          |

大家好, 我正在尝试从这个 table(我们称之为计费 table)和 inner/left 加入其他 tables 基于从 subid_values 获得多个 where 条件。

例如使用subid_name = school_loc returns subid_value of 41000,如果我加入它,我可以从这里获得更多关于学校位置的信息进入 location_info table.

但是,当我还需要 return 原始帐单中的 return 值 fee_sem 和 CGPA_grade 以及 school_type 时,问题就来了 table作为查询结果的一部分,因为我已经使用了“where subid_name = school_loc”。

我也想加入基于不同 subid_values 的其他 tables,这些 subid_name(s ) 如 school_type、fee_sem、CGPA_grade

我试过并哭过试图根据主 ID 自我加入自己,我也尝试过嵌套 select 但没有成功。有人告诉我,这就是数据的样子,并且开发人员不会更改更正 table 结构,该结构应该首先被转置和列化。通常 MS SQL 和结构更好的数据库没有问题,但是这是在 MySQL 数据库上完成的,我不太擅长使用 MySQL,但是行数据应该有一直在专栏,我需要寻求帮助。

select * from billing
where main_id = 100
and subid_name = **'school_loc'**

所以这将 return 一行只有 subid_value = 41000

我会将此 subID 值location_info table 和 select 内联location_info table 中的列 (location_name, location_state) 使用 billing.subid_value = 41000

的值
select billing.mainID
,billing.subID
,billing.subid_value as School_Postcode 
,location_info.location_name
,location.info,location_state
from dbo.billing
inner join dbo.location_info on billing.subid_value = location_info.ID
where subid_name is billing.school_loc

确定,第一个内部联接已完成(基于 billing.subid_name = school_loc 的位置)。 我被困在这里,我需要将下面的内容与 subid_name 的不同 where 结合起来,并期待不同的 subid_value 并将结果 subid_value 放入内连接

select billing.mainID
,billing.subID
,billing.subid_value as Fee_Class
,fee_ranking.FeeAffordable
,fee_ranking.FeeSubsidy
inner join dbo.fee_ranking on billing.subid_value = fee_ranking.class
where billing.main_id = 100
and billing.subid_name = **'fee_sem'**

所以这只会 return 一行(fee_sem = 40 的那一行) (与另一个内部联接组合成 fee_ranking_table)

我还想合并多个 where of subid_name 并期望不同的 subid_value 并将结果 subid_value 放入一个内连接

select billing.mainID
,billing.subID
,billing.subid_value as CGPA_Score
,CGPA_ranking.IsSmart
,CGPA_ranking.IsHardToEnter
inner join dbo.CGPA_ranking on billing.subid_value = CGPA_ranking.score
where billing.main_id = 100
and billing.subid_name = **'CGPA_grade'**

所以这将 return 只有一行(CGPA_grade = 2.9 的那一行)

我正在尝试实现

的输出
  | mainid(from billing) | school_postcode | location_name (from location_info) | location_state (from location_info) | Fee_Class | FeeAffordable (from fee_ranking) | CGPA_Score | IsSmart (from CGPA_ranking) |
  -----------------------|-----------------|------------------------------------|-------------------------------------|-----------|----------------------------------|------------|-----------------------------|
  |100                   |41000            |Boston                              |MA                                   |40         |False                             |2.9         |False                        |

这有点乏味,但您只需要有与数据中的 subid_names(或可能是 subids)一样多的连接(或可能是左连接),并为每个连接分配一个别名,以便您可以添加来自其他表的数据。

with cte as
(select mainid from t where subid_name = 'school_loc')
select cte.mainid,t1.subid_value,t2.subid_value,t3.subid_value,
                       t4.subid_value,t5.subid_value,t6.subid_value
from cte
join t t1 on t1.mainid = cte.mainid and t1.subid_name = 'name'
join t t2 on t2.mainid = cte.mainid and t2.subid_name = 'school_type'
join t t3 on t3.mainid = cte.mainid and t3.subid_name = 'school_loc'
join t t4 on t4.mainid = cte.mainid and t4.subid_name = 'fee_sem'
join t t5 on t5.mainid = cte.mainid and t5.subid_name = 'desc'
join t t6 on t6.mainid = cte.mainid and t6.subid_name = 'cgpa_grade';

+--------+-------------+-------------+-------------+-------------+-------------+-------------+
| mainid | subid_value | subid_value | subid_value | subid_value | subid_value | subid_value |
+--------+-------------+-------------+-------------+-------------+-------------+-------------+
|    100 | Ali Baba    | ghetto      | 41000       | 40          | thieves     | 2.9         |
+--------+-------------+-------------+-------------+-------------+-------------+-------------+
1 row in set (0.001 sec)