Oll*_*lie 2 mysql join data-warehouse
每个FACT_CRIME都有一个分配的DIM_OFFICER; 每个DIM_OFFICER都分配给一个DIM_STATION。
我可以使用什么 SQL 逻辑来显示所有FACT_CRIMEs 以及关联的 s DIM_STATION?我的问题是要获得DIM_STATION. station_code,我知道它必须通过 完成DIM_OFFICER,因为那是关系链接,但我对 SQL 不够精明,不知道如何从 2 个表中获取数据。
这是我到目前为止的一个查询,它产生了我所追求的格式,但不是正确的结果:
select
fact_crime.*, dim_station.station_code
from fact_crime
join dim_station
on fact_crime.assigned_officer_id
in (select dim_officer.officer_id from dim_officer where dim_officer.station_id = dim_station.station_id)
group by dim_station.station_code
Run Code Online (Sandbox Code Playgroud)
我期待每一个实例都FACT_CRIME出现,指定的官员的车站代码也会出现在表格中。
提前感谢任何能指出我正确方向的人。我仍在尝试掌握 SQL 的基础知识,因此如果我需要对任何回复进行额外说明,我深表歉意。
你需要两个连接,就像这样(我猜是因为你没有显示很多细节)。
...
from fact_crime c
join dim_officer o ON c.assigned_officer_id = o.officer_id
join dim_station s ON o.station_id = s.station_id
Run Code Online (Sandbox Code Playgroud)
这是一种非常常见的 SQL 模式,因此您会发现 MySQL 软件在提高效率方面做得很好。
专业提示:避免*in SELECT *,尤其是来自大量JOIN操作结果的结果集。相反,用于SELECT提供您需要的列列表。