请求基于三张关联表创建含指定字段的数据库视图
Create View with Joined KPI and Program Details
Got it, let's build the view you need step by step. The key here is to leverage joins between your junction table and the two main tables to pull in the names alongside the IDs you need.
Quick Breakdown of the Approach:
- We'll link the
kpi_per_progjunction table to your program table (I'll assume it's namedprograms—swap this with your actual "表2" name if it's different) using the sharedprog_id - Then join the result to your KPI table (assumed
kpis—replace with your actual "表1" name if needed) viakpi_id - Alias the ID fields to match your requested
id_progandid_kpilabels - Since
prog_idandkpi_idform a composite primary key onkpi_per_prog, this pair will remain unique in the resulting view
SQL Code for the View:
CREATE VIEW kpi_program_associations AS SELECT kpp.prog_id AS id_prog, kpp.kpi_id AS id_kpi, p.prog_name, k.kpi_name FROM kpi_per_prog kpp INNER JOIN programs p ON kpp.prog_id = p.prog_id INNER JOIN kpis k ON kpp.kpi_id = k.kpi_id;
Quick Notes:
- If you want to include programs or KPIs that don't have any associations (i.e., no entry in
kpi_per_prog), swapINNER JOINwithLEFT JOIN - Make sure to replace
programsandkpiswith your actual table names if they differ from what I used
内容的提问来源于stack exchange,提问作者saad
相关产品推荐
相关产品推荐

