0

计费表

 | 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                        |
4

1 回答 1

0

这有点乏味,但您只需要在数据中拥有与 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)
于 2020-11-30T14:54:40.003 回答