简体   繁体   中英

How to select data from one table and get all associated records from another table?

I am trying to simplify my question to best that I can, I hope my explanation will clarify what it means.

The scenario is this:

I have two tables namely: tbl_instructors which has the instructors information such as instructor ID and Name and tbl_advisory which has the all the records of the class that is being handled by a specific instructor like the Course , Year and Section of the class. So my table is something like this:

tbl_instructor:

id  | name
----+--------------
001 | Jhon Doe
002 | Kaitlyn Moore  

tbl_advisory:

instructor_id  | course        | year      |  section
---------------+---------------+-----------+-----------
001            | BSINT         | 2nd Year  | A
001            | BSINT         | 2nd Year  | C
001            | BSINT         | 2nd Year  | B
002            | BSBA          | 1st Year  | A
002            | BSBA          | 1st Year  | D

Now, Im trying to select the instructor Id and Name and its associated advisory information using INNER JOIN in my PHP query:

<?php
$getAdvisory = "SELECT 
  ti.id AS id,
  ti.name AS name,
  ta.course AS ccourse,
  ta.year AS cyear,
  ta.section AS csection
FROM tbl_instructor as ti
INNER JOIN tbl_advisory as ta
ON ti.id = ta.instructor_id";
OpenConn()->query($getAdvisory);

if ($getAdvisory->num_rows > 0) {
    while ($row = $getAdvisory->fetch_assoc()) {
     $id = $row['id'];
     $name = $row['name'];
    }
    ?>
    <td><?php echo $id; ?></td>
    <td><?php echo $name; ?></td>
    <td><button>View Advisory</button></td>
    <?php
 }
}
?>

Which gives me the result set in my html/php table:

id   | name          |  action
-----+---------------+-----------
001  | John Doe      | View Advisory
001  | John Doe      | View Advisory
001  | John Doe      | View Advisory
002  | Kaitlyn Moore | View Advisory
002  | Kaitlyn Moore | View Advisory

My problem is I want my html table to be DISTINCT by Instructor ID but still return all the the associated advisory information of each Instructor because I want to display what class the instructor is holding in a modal when I click the View Advisory button next to every Instructors Name So that I can update my table if the Instructor has removed or added a new Advisory.

How can I achieve it ? Thanks in advance.

You need DISTINCT clause

 SELECT DISTINCT 
  ti.id AS id,
  ti.name AS name,
  ta.course AS ccourse,
  ta.year AS cyear,
  ta.section AS csection
FROM tbl_instructor as ti
INNER JOIN tbl_advisory as ta ON ti.id = ta.instructor_id

In this way you should obtain

id   | name          |  action
-----+---------------+-----------
001  | John Doe      | View Advisory
002  | Kaitlyn Moore | View Advisory

If the value are on different rows the you could use a (fake) aggregation function

SELECT ti.id AS id,
ti.name AS name,
min(ta.course) AS ccourse,
min(ta.year) AS cyear,
min(ta.section)  AS csection
FROM tbl_instructor as ti
INNER JOIN tbl_advisory as ta ON ti.id = ta.instructor_id
GROUP BY id, name 

Plis check this query :

SELECT    ti.*, GROUP_CONCAT( CONCAT( ta.course, ' ' , ta.year,' ', ta.section ) separator ' - ') as ColumnName 
FROM tbl_instructor as ti
INNER JOIN tbl_advisory as ta ON ti.id = ta.instructor_id 
GROUP BY ti.idid  

Output:

      | name          |  action
 -----+---------------+-----------
 001  | John Doe      | BSINT 2nd Year a - BSINT 2nd Year b - BSINT 2nd Year c
 002  | Kaitlyn Moore | BSBA 1st Year d - BSBA 1st Year a

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