列標籤上我有兩個表如下/透視:列名和列標題和計算基於其他表
表marklist:
student_id class_id subject_1 subject_2 subject_3 subject_4 subject_5
----------- ----------- ----------- ----------- ----------- ----------- ----------
1 9 78 87 95
2 9 67 95 87
3 9 85 84 85
4 10 70 65 78
5 10 75 80 81
6 10 80 75 82
表subject_names
column_name subject_name
--------------- -------------
subject_1 English
subject_2 Chemistry
subject_3 Economics
subject_4 Accounts
subject_5 Biology
現在,我需要生成一個像這樣的報告class_id = 9
column_name subject_name no_of_students
--------------- ------------- --------------
subject_1 English 3
subject_2 Chemistry 3
subject_3 Economics 0
subject_4 Accounts 0
subject_5 Biology 3
總之,我必須生成一個帶有column_names,subject_name和來自class_id = 9(或10,無論)的學生人數的報告。
所有我設法做的是
1.
SELECT sn.column_name, sn.subject_name FROM subject_names sn;
和
2.
SELECT ml.class_id,
count(ml.subject_1) AS s1,
count(ml.subject_2) AS s1,
count(ml.subject_3) AS s1,
count(ml.subject_4) AS s1,
count(ml.subject_5) AS s1,
FROM marklist ml
WHERE ml.class_id = 9;
我不明白我怎麼繼續前進,將查詢2的結果與查詢1一起傳送。我可能會走錯方向,但我不知道。
從長遠來看,你很可能會被更好的進一步正常化您的數據服務;重複列往往是設計缺陷的標誌。 –
@ Clockwork-Muse-有時候我們都有義務和事物一起生活,而不改變它們,是不是?這是我的一個時代。 – Rachcha