2014-11-23 56 views
1

我有這樣的:多列外鍵引用

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是一個外鍵引用到這是sc_id從表subject_current相同的密鑰。並且列sl_id來自表subject_current參照sl_id來自表subject_list

我的問題是,我怎麼能檢索使用僅從table時間表將數據從tablesubject_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。所以,如果你能提出一些建議,我很樂意欣賞。

編輯

我發現這一點,我幾乎沒有,但只顯示錶中的第一sc_idschedule_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>"; 

?> 
+1

@F abioCardoso,感謝回覆先生。但是sched_id不匹配sc_id。想象一下,你有一個有很多主題ID的時間表,並且主題ID保存來自另一個表的數據。 – 2014-11-23 18:01:10

+0

像'idn'的氣味列,您需要通過創建其他表來對其進行標準化 – 2014-11-23 20:30:39

回答

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

哇!這對我來說現在是有意義的。從今以後,這對我來說是一個很好的做法。但我對「主題」,「主題」,「sced_id」和「sced_name」感到困擾。 「sced_id」和「sced_name」可能是拼寫錯誤。但是「主題」和/或「主題」,我不明白。你是從哪裏做的?我很困惑。 – 2014-11-24 03:17:14

+0

我已經嘗試過您查詢並更改某些內容以使其正常工作,但它僅顯示錶** schedule_subject_currents **中的第一個'sc_id'。如果我刪除了「GROUP BY」,則會顯示所有的「sc_id」。但我希望它被分組,看起來像我的問題上的表格。 – 2014-11-24 04:08:40

+0

@ArchieZineg當你從這個答案中得到一個查詢和模式爲你工作時,一定要不接受另一個醜陋的非關係型答案並接受這個答案。 – philipxy 2014-11-24 09:30:00

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

?>