简体   繁体   中英

Inner Join select only two records from mysql

I have a subject and a departments table, Each subject is associated with departments table. I am trying to select all subjects including departments name. The code bellow work perfect but show only two records. Any Help will be appreciated

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

Please try the following. I think you may have used the wrong fields in the join condition:

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

A "foreign key" in the subjects table for department is much more likely to be subjects.dep_id

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