计费表
| 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 |
大家好,我正在尝试根据从多个 where 条件获得的 subid_values 从这个表(我们称之为计费表)和内部/左连接到其他表中工作。
例如,使用 subid_name = school_loc 返回 subid_value 为 41000,如果我将其加入 location_info 表,我可以从这里获得有关学校位置的更多信息。
但是,当我还需要从原始计费表中返回诸如 fee_sem 和 CGPA_grade 和 school_type 之类的值作为查询结果的一部分时,问题就来了,因为我已经使用了“where subid_name = school_loc”。
我还想加入其他基于不同 subid_values 的表,这些 subid_values基于不同的 subid_name(s),如 school_type、fee_sem、CGPA_grade
我曾尝试并哭着尝试根据主 ID 自行加入,我也尝试过嵌套选择但没有取得多大成功。有人告诉我,这就是数据的样子,并且开发人员不会更改更正表结构,该表结构应该首先被转置和列化。通常 MS SQL 和结构更好的数据库没有问题,但是这是在我不擅长使用 MySQL 的 MySQL 数据库上完成的,但是对于应该在列中的行数据,我需要寻求帮助。
select * from billing
where main_id = 100
and subid_name = **'school_loc'**
所以这将只返回一行 subid_value = 41000
我将这个subID 值与location_info表内部连接,并使用 billing.subid_value = 41000 的值从 location_info 表中选择更多列(location_name,location_state)
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
OK 第一次内部连接完成(基于 where billing.subid_name = school_loc)。我被困在这里,我需要将以下内容与 subid_name 的不同位置结合起来,并期望不同的 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'**
所以这将只返回一行(fee_sem = 40 的那一行)(与另一个内部连接结合到 fee_ranking_table 中)
我还想组合多个subid_name 的 where 并期望不同的 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'**
所以这将只返回一行(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 |