简体   繁体   中英

Multiple column foreign key referencing on the same key from a table

I have this:

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

Column sc_id1 - sc_id5 is a foreign key referencing to the same key which is sc_id from table subject_current . And column sl_id from table subject_current is referencing to sl_id from table subject_list .

My question is, how can I retrieve the data from table subject_list using only the data from table schedules ? What I want to achieve is like this:

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

I've search about LEFT JOIN but it's kinda hard for me because I'm just a beginner in this field and there's three tables that may have to JOIN . So please if you can tip some advice I gladly appreciate that.

EDITED

I've found this and I'm almost there, but it only shows the first sc_id from table 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>";

?>

Avoid storing the same kind of information in different columns, otherwise you end up with problems like the one you have encountered. Instead you should store each piece of information in its own row.

First, normalize your data schema by introducing another table called eg scedule_subject_currents, so that you have

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

and

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

Then you can use the query

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

Try while nesting your query:

<?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>";

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM