我有這樣的:多列外鍵引用
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
時間表將數據從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
。所以,如果你能提出一些建議,我很樂意欣賞。
編輯
我發現這一點,我幾乎沒有,但只顯示錶中的第一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>";
?>
@F abioCardoso,感謝回覆先生。但是sched_id不匹配sc_id。想象一下,你有一個有很多主題ID的時間表,並且主題ID保存來自另一個表的數據。 – 2014-11-23 18:01:10
像'idn'的氣味列,您需要通過創建其他表來對其進行標準化 – 2014-11-23 20:30:39