我有这样的:多列外键引用
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