其他表基本上我有一個查詢,如下所示:加入參考表比基表
SELECT MAIN.response_id AS 'Response Id',
CONCAT(CASE
WHEN MAIN.months = 'Jan - Mar' THEN 'March'
WHEN MAIN.months = 'Apr - Jun' THEN 'June'
WHEN MAIN.months = 'Jul - Sep' THEN 'September'
WHEN MAIN.months = 'Oct - Dec' THEN 'December'
ELSE 'Error'
END, ' ', MAIN.Year) AS Period,
CNT.country_name AS Country, INITM.num_modules AS 'Initial Training - Number of Modules', INITTRAINED.num_instr AS 'Initial Training - Instructors Trained', INITPASS.pass_num AS 'Initial Training - Instructors Passed', INITPASS.pass_num/INITTRAINED.num_instr AS 'Initial Training - Pass Percentage'
FROM responses_main AS MAIN -- Main responses table
LEFT OUTER JOIN responses_init_training_modules AS INITM USING (response_id) -- Main INIT training table
LEFT OUTER JOIN responses_init_training_pass_num AS INITPASS USING (response_id) -- Main INIT training table
LEFT OUTER JOIN responses_init_training_instr_trained AS INITTRAINED USING (response_id) -- Main INIT training table
LEFT OUTER JOIN country AS CNT ON MAIN.country_id = CNT.country_id -- Country table
GROUP BY MAIN.response_id, MAIN.months + ' ' + MAIN.Year, CNT.country_name
什麼從該查詢缺少的是一個連接到節目表。每個引用表(INITM,INITPASS和INITTRAINED)都有一個引用程序表的program_id,但是基表(MAIN)與程序表無關。當前查詢的結果是隻返回第一個數據的表格program_id +不顯示任何程序信息。
如何獲取顯示MAIN.response_id,PROG.program_id,INITM.num_modules,INITPASS.pass_num和INITTRAINED.instr_trained的數據集?我無法弄清楚如何使用3個引用表中的數據來獲取一個引用這兩個維度(MAIN和PROG)的表格。
我畫了什麼,我想在這裏實現:
任何幫助深表感謝。
不要畫出來。平方米。 – Strawberry
你寫過3個表中有program_id作爲外鍵。這3個表中的哪一個應引用程序表? – Shadow
@shadow其respond_init_training_modules,respond_init_training_pass_num和respond_init_training_instr_trained表。 respond_main表沒有program_id列。 –