2017-07-27 25 views
0

後通過PHP組或人造黃油SQL行有一個MySQL表像波紋管我如何查詢

id | roll | exam_id | course_id | marks | status 
---------------------------------------------------------- 
1 | 01001 |  1 |  1  | 56 | 1 
2 | 01002 |  1 |  1  | 68 | 1 
3 | 01003 |  1 |  1  | 55 | 1 
4 | 01004 |  1 |  1  | 67 | 1 
5 | 01001 |  1 |  2  | 54 | 1 
6 | 01002 |  1 |  2  | 59 | 1 
7 | 01003 |  1 |  2  | 62 | 1 
8 | 01004 |  1 |  2  | 63 | 1 
9 | 01001 |  2 |  3  | 61 | 1 
10 | 01002 |  2 |  3  | 48 | 1 
11 | 01003 |  2 |  3  | 22 | 1 
12 | 01004 |  2 |  3  | 39 | 1 

現在我想將所有的行與exam_id = 1

SELECT * FROM result WHERE exam_id=1 ORDER BY course_id 

之後,我需要在將其分組爲roll後,將該表格顯示在HTML內部表示按照結果表的行程編號具有行跨距的一行

Roll | course_id | marks 
----------------------------- 
01001 |  1  | 56 
     |  2  | 68 
----------------------------- 
01002 |  1  | 55 
     |  2  | 67 
----------------------------- 
01003 |  1  | 55 
     |  2  | 62 
----------------------------- 
01004 |  1  | 67 
     |  2  | 63 

我正在使用Codeigniter框架來做這個項目。有關我如何才能做到這一點的任何建議?

預先感謝您。

[編輯] 當前SQL我使用這樣做:

SELECT * FROM `exam_result` JOIN `course` ON `course`.`course_tab_id`=`exam_result`.`result_course` WHERE `exam_id` = '1' AND `result_status` = 1 GROUP BY `exam_result`.`exam_roll`, `course`.`course_tab_id` ORDER BY `exam_result`.`exam_roll` ASC, `course`.`course_id` ASC 
+2

爲什麼在SQL查詢本身滾你不組它,只是使用這些數據綁定到你的HTML表格或任何你在UI – Sujith

+0

使用'array_merge'使用顯示器。 –

+0

只需按roll和course_id命令'''SELECT * FROM result WHERE exam_id = 1 ORDER BY roll,course_id''' – Neodan

回答

0

試試這個,但我的第一次查詢該表中的差異是課程標題​​將包含在一張桌子上,我已將標記設置爲AVG,因此如果您有重複的數據,您將看到他們的平均成績爲學校的課程和學習卷或任何您使用此結構的內容。或

SELECT roll, exam_id, a.course_id, marks, status, course_title, course_credit 
FROM 
(SELECT roll, exam_id, course_id, AVE(marks) as marks, status FROM result) as a 
LEFT JOIN 
(SELECT course_id, course_title, course_credit FROM course) as b 
ON 
a.course_id = b.course_id 
WHERE exam_id = '1' -- you can remove this if you wanted all exam appear on your list 
GROUP BY roll, exam_id, a.course_id, marks, status, course_title, course_credit 
ORDER BY roll, course_id, marks 
+0

儘管這並不能解決我的問題,但我得到了一個解決方案從這個答案。所以謝謝:) – WebDevRon

+0

你找到的答案是什麼^^ _你能分享嗎? –

0

試試這個sql查詢

SELECT Roll, course_id, AVG(marks) 
FROM result WHERE exam='1' 
GROUP BY Roll, course_id 
ORDER BY roll, course_id, marks 
+0

這是我當前的SQL查詢。 'SELECT * FROM'exam_result' JOIN'course' ON'course'.'course_tab_id' ='exam_result'.'result_course' WHERE'exam_id' ='1'AND'result_status' = 1 GROUP BY'exam_result'.'exam_roll' ,'course'.'course_tab_id' ORDER BY'exam_result'.'exam_roll' ASC,'course'.'course_id' ASC ' – WebDevRon

+0

你有多少桌子,爲什麼你只用JOIN,如果你只有一張桌子。你可以展示你的桌子的結構嗎? –

+0

其實我有2個連接表。我只發佈了我的問題的簡化結構。 – WebDevRon