我有一个科目和一个科目表,每个科目都与科目表相关联。我正在尝试选择所有科目,包括部门名称。代码下面的工作完美,但只显示两个记录。任何帮助将不胜感激
//Select statement
$selects=$connection->query("SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
ON subjects.related_to = departments.dep_id");
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th>Sub Id</th>
<th>Subject Name</th>
<th>Related to Department</th>
<th>Related to Semester</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
while($result=$select->fetch_assoc()) {
?>
<tr>
<td><?php echo $result['id']; ?></td>
<td class="center"><?php echo $result['name']; ?></td>
<td class="center"><?php echo $result['dept']; ?></td>
<td class="center"><?php echo $result['related_to_sem']; ?></td>
<td class="center">
<a class="btn btn-info" href="#">
<i class="icon-edit icon-white"></i>
Edit
</a>
<a class="btn btn-danger" href="#">
<i class="icon-trash icon-white"></i>
Delete
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
答案 0 :(得分:0)
select
subjects.id,subjects.name,
subjects.related_to,
subjects.related_to_sem,
departments.dept
from
subjects
LEFT OUTER JOIN departments ON subjects.id=departments.dep_id
答案 1 :(得分:0)
请尝试以下方法。我想你可能在连接条件中使用了错误的字段:
SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
/* ON subjects.id = departments.dep_id */
ON subjects.dep_id = departments.id
部门科目表中的“外键”更有可能成为subjects.dep_id