I have the following database tables and columns:
students
+------------+-------------------+------------------+
| student_id | student_firstname | student_lastname |
+------------+-------------------+------------------+
| 95 | John | Doe |
+------------+-------------------+------------------+
studentcourseplan
+--------+------------+
| cpl_id | student_id |
+--------+------------+
| 209 | 95 |
| 273 | 95 |
+--------+------------+
studentdates
+------------+-------------------+-----------------+
| student_id | student_startdate | student_enddate |
+------------+-------------------+-----------------+
| 95 | 2012-07-02 | 2012-08-17 |
| 95 | 2012-08-20 | 2012-11-16 |
+------------+-------------------+-----------------+
If I run this query…
SELECT
scp.cpl_id,
s.student_id,
s.student_firstname,
s.student_lastname,
sd.student_startdate,
sd.student_enddate
FROM
studentcourseplan scp
INNER JOIN
students s ON s.student_id = scp.student_id
INNER JOIN
studentdates sd ON sd.student_id = s.student_id
… I get the following output:
+--------+------------+-------------------+------------------+-------------------+-----------------+
| cpl_id | student_id | student_firstname | student_lastname | student_startdate | student_enddate |
+--------+------------+-------------------+------------------+-------------------+-----------------+
| 209 | 95 | John | Doe | 2012-07-02 | 2012-08-17 |
| 273 | 95 | John | Doe | 2012-07-02 | 2012-08-17 |
+--------+------------+-------------------+------------------+-------------------+-----------------+
Notice the dates from result compared to the values in the table studentdates. They are wrong. I want something like the following output instead:
+--------+------------+-------------------+------------------+-------------------+-----------------+
| cpl_id | student_id | student_firstname | student_lastname | student_startdate | student_enddate |
+--------+------------+-------------------+------------------+-------------------+-----------------+
| 209 | 95 | John | Doe | 2012-07-02 | 2012-08-17 |
| 273 | 95 | John | Doe | 2012-08-20 | 2012-11-16 |
+--------+------------+-------------------+------------------+-------------------+-----------------+
What am I doing wrong?
You need to map
cpl_idwithstudentdates. You can add one column tostudentdatescalled cpl_id & then change the query accordingly.