Reputation: 39
I have three tables that link together. What I want to do is to generate a report card for all the students in a class.
table students_info
name sex age students_ID
--------- --- --- -----------
Kinsley M 12 1
Michael m 12 2
Rhianna f 22 3
Table scores_panel
1stCA 2ndCA exam students_ID subjectID
----- ----- ---- ----------- ---------
23 15 42 1 1
10 12 7 1 2
43 15 62 1 3
10 12 27 2 1
10 12 57 2 2
23 15 12 2 3
11 12 27 3 1
04 12 57 3 2
13 25 12 3 3
Table subject
subjectname subjectID
----------- ---------
english 1
maths 2
biology 3
I want my result to look like this:
NAME KINSLEY
SEX M
AGE 12
and report card follow
subject 1stCA 2ndCA EXAM
--------- ----- ----- ----
english 23 15 42
maths 10 12 7
Biology 43 15 62
... and so on for all students
only one subject and scores retrieved instead of all
<?php
include("connect.php");
$generate="SELECT students_info.name, subject.subjectname, scores_panel.1stCA, scores_panel.2ndCA, scores_panel.EXAM
FROM
students_info
LEFT JOIN
scores_panel
ON students_info.students_ID=scores_panel.students_ID
LEFT JOIN
subject
ON
subject.subjectID=scores_panel.subjectID ";
$fetch=mysql_query($generate);
while($row=mysql_fetch_array($fetch)or die(mysql_error()))
{
?>
**NAME:**
<?PHP echo $row['name']; ?>
subject 1stCA 2ndCA EXAM
----------
<?PHP echo $row['subjectname']; ?>
<?PHP echo $row['1stCA']; ?>
<?PHP echo $row['2ndCA']; ?>
<?PHP echo $row['EXAM']; ?>
THIS IS YOUR REPORT CARD
<?PHP } ?>
It works, but displays only one subject for each student, instead of something like this:
NAME KINSLEY
SEX M
AGE 12
and report card follow
subject 1stCA 2ndCA EXAM
--------- ----- ----- ----
english 23 15 42
maths 10 12 7
Biology 43 15 62
NAME Rhianna
SEX F
AGE 22
and report card follow
subject 1stCA 2ndCA EXAM
--------- ----- ----- ----
english 11 12 27
maths 04 12 57
Biology 13 25 12
... and so on for all students.
Upvotes: 2
Views: 184
Reputation: 7678
Here is SQLFiddle
And Query is
SELECT T1.NAME, T1.SEX, T1.AGE, T2.EXAM, T3.SUBNAME, T2.1CA, T2.2CA
FROM TAB1 AS T1 LEFT JOIN TAB2 AS T2 ON T1.STUDENT_ID = T2.STUDENT_ID
LEFT JOIN TAB3 AS T3 ON T2.SUBJ_ID = T3.SUBID;
Upvotes: 1
Reputation: 551
Simple MySQL left join should solve this:
SELECT
st.name, st.sex, st.age,
sub.subjectname,
sc.1stCA, sc.2ndCA, sc.exam
FROM scores_panel AS sc
LEFT JOIN subject AS sub ON sub.subjectID = sc.subjectID
LEFT JOIN students_info AS st ON st.students_ID = sc.students_ID
Upvotes: 1
Reputation: 21
Have you red this? http://dev.mysql.com/doc/refman/5.7/en/join.html
If you already know about Table Joins, then you have to precise/show us what your problem is exactly (meaning what is the current status of your code)
Upvotes: 1