0

我有以下数据库表和列:

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      |
+------------+-------------------+-----------------+

如果我运行此查询...

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

...我得到以下输出:

+--------+------------+-------------------+------------------+-------------------+-----------------+
| 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      |
+--------+------------+-------------------+------------------+-------------------+-----------------+

请注意与表中的值相比的结果日期studentdates。他们错了。我想要类似以下输出的东西:

+--------+------------+-------------------+------------------+-------------------+-----------------+
| 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      |
+--------+------------+-------------------+------------------+-------------------+-----------------+

我究竟做错了什么?

4

1 回答 1

1

您需要cpl_id使用studentdates. 您可以向studentdates名为 cpl_id 的列添加一列,然后相应地更改查询。

于 2012-09-24T06:55:36.673 回答