从表中的同一个键引用多列外键

时间:2014-11-23 17:36:35

标签: mysql database-normalization

我有这个:

Table "schedules"
----------------------------------------------------------------------
| sched_id | sched_name | sc_id1 | sc_id2 | sc_id3 | sc_id4 | sc_id5 |
----------------------------------------------------------------------
|    1     |  block 1   |   1    |    2   |   3    |    4   |    5   |
----------------------------------------------------------------------
|    2     |  block 2   |   1    |    2   |   3    |  NULL  |  NULL  |
---------------------------------------------------------------------- 

Table "subject_current"
---------------------------------------
|sc_id | sl_id | schoolyear | semister|
---------------------------------------
|   1  |   5   | 2014-2015  |    1st  |
---------------------------------------
|   2  |   6   | 2014-2015  |    1st  |
---------------------------------------
|   3 |    7   | 2014-2015  |    1st  |
---------------------------------------
|   4  |   8   | 2014-2015  |    1st  |
---------------------------------------
|   5  |   9   | 2014-2015  |    1st  |
---------------------------------------

Table "subject_list"
-------------------------------------------------------------
|sl_id | subject_code | subject_description | subject_prereq|
-------------------------------------------------------------
|   5  |      math1   |         algebra     |      none     |
-------------------------------------------------------------
|   6  |      math2   |    trigonometry     |      none     |
-------------------------------------------------------------
|   7  |      math3   |        Calculus     |      none     |
-------------------------------------------------------------
|   8  |      eng1    |       english 1     |      none     |
-------------------------------------------------------------
|   9  |      hum1    |      humanities     |      none     |
-------------------------------------------------------------

sc_id1 - sc_id5 是引用表 subject_current 中相同键 sc_id 的外键。表 subject_current 中的 sl_id 列引用表 subject_list 中的 sl_id

我的问题是,如何仅使用table 时间表中的数据从table subject_list 检索数据?我想要实现的是这样的:

(This will echo on my page)
--------------------------------------------------------------------------------
| sched_id | sched_name |                       Subjects                       | 
--------------------------------------------------------------------------------
|    1     |   block 1  | algebra, tirgonometry, calculus, english1, humanities|
--------------------------------------------------------------------------------
|    2     |   block 2  |          algebra, tirgonometry, calculus             |
--------------------------------------------------------------------------------

我搜索了LEFT JOIN,但这对我来说有点困难,因为我只是这个领域的初学者而且有三个tables可能必须JOIN。所以,如果你能提出一些建议,我很高兴地欣赏它。

EDITED

我发现了这一点,我几乎就在那里,但它只显示表格中的第一个sc_id schedule_subject_currents

    <?php

    echo "<table class='opensubtbl' cellspacing='0' cellpadding='5' ><tr>";

    echo "<th>Schedule ID</th>";
    echo "<th>Sched Name</th>";
    echo "<th>Subjects ID</th></tr>";

    //$sched = mysql_query("select * from schedules s, schedule_subject_currents h, subject_current c, subject_list l where s.sched_id = h.sched_id and h.sc_id = c.sc_id and c.sl_id = l.sl_id");
    //$sched = mysql_query("SELECT * FROM schedules");

    $sched = mysql_query("

        SELECT *
        FROM schedules s
        LEFT JOIN schedule_subject_currents ssc ON ssc.sched_id = s.sched_id
        LEFT JOIN subject_current c ON c.sc_id = ssc.sc_id
        LEFT JOIN subject_list l ON l.sl_id = c.sl_id
        GROUP BY s.sched_id, sched_name
        ORDER BY sched_name

    ");

    while($rows_s = mysql_fetch_assoc($sched)){
        $s_schedid = $rows_s['sched_id'];
        $s_schedname = $rows_s['sched_name'];
        $s_subdesc = $rows_s['subject_description'];
        $s_scid = $rows_s['sc_id'];

        echo "<tr>";
        echo "<td>$s_schedid</td>";
        echo "<td>$s_schedname</td>";

        echo "<td>$s_subdesc</td>";
        echo "</tr>";
    }

    echo "</table>";

?>

2 个答案:

答案 0 :(得分:2)

避免在不同的列中存储相同类型的信息,否则您最终会遇到类似问题的问题。相反,您应该将每条信息存储在自己的行中。

首先,通过引入另一个名为例如 scedule_subject_currents的表来规范化您的数据模式,以便您拥有

Table "schedules"
-------------------------
| sched_id | sched_name |
-------------------------
|    1     |  block 1   | 
-------------------------
|    2     |  block 2   | 
-------------------------

Table "schedule_subject_currents"
--------------------
| sched_id | sc_id |
--------------------
|    1     |  1    |
--------------------
|    1     |  2    |
--------------------
|    1     |  3    |
--------------------
|    1     |  4    |
--------------------
|    1     |  5    |
--------------------
|    2     |  1    |
--------------------
|    2     |  2    |
--------------------
|    2     |  3    |
--------------------

然后您可以使用查询

SELECT s.sched_id, s.sched_name, GROUP_CONCAT(subject_description) as Subjects
FROM schedules s
LEFT JOIN schedule_subject_currents ssc ON ssc.sched_id = s.sched_id
LEFT JOIN subject_current c ON c.sc_id = ssc.sc_id
LEFT JOIN subject_list l ON l.sl_id = c.sl_id
GROUP BY s.sched_id, sched_name
ORDER BY sched_name

答案 1 :(得分:1)

尝试while嵌套查询:

<?php

    echo "<table class='opensubtbl' cellspacing='0' cellpadding='5' ><tr>";

    echo "<th>Schedule ID</th>";
    echo "<th>Sched Name</th>";
    echo "<th>Subjects</th>";
    echo "<th></th></tr>";

    //$sched = mysql_query("select * from schedules s, schedule_subject_currents h, subject_current c, subject_list l where s.sched_id = h.sched_id and h.sc_id = c.sc_id and c.sl_id = l.sl_id");
    //$sched = mysql_query("SELECT * FROM schedules");
    //$sched = mysql_query("SELECT * FROM schedules s LEFT JOIN schedule_subject_currents ssc ON ssc.sched_id = s.sched_id LEFT JOIN subject_current c ON c.sc_id = ssc.sc_id LEFT JOIN subject_list l ON l.sl_id = c.sl_id ORDER BY sched_name ");

    $sched = mysql_query("SELECT * FROM schedules");

    while($rows_s = mysql_fetch_assoc($sched)){
        $s_schedid = $rows_s['sched_id'];
        $s_schedname = $rows_s['sched_name'];

        echo "<tr>";
        echo "<td>$s_schedid</td>";
        echo "<td>$s_schedname</td>";
        echo "<td>";

        $schedsubcur = mysql_query("SELECT * FROM schedule_subject_currents WHERE sched_id='$s_schedid'");
        while($rows_ssc = mysql_fetch_assoc($schedsubcur)){
            $ssc_scid = $rows_ssc['sc_id'];
            $schedsublist = mysql_query("SELECT * FROM subject_current WHERE sc_id='$ssc_scid'");
            while($rows_ssl = mysql_fetch_assoc($schedsublist)){
                $ssl_slid = $rows_ssl['sl_id'];
                $ssublist = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ssl_slid'");
                while($rows_ssubl = mysql_fetch_assoc($ssublist)){
                    $ssubl_subdesc = $rows_ssubl['subject_description'];
                    echo $ssubl_subdesc."($ssc_scid), ";
                }
            }
        }

        echo "</td>";
        echo "</tr>";
    }

    echo "</table>";

?>