DigitalDesigner DigitalDesigner -3 years ago 111
MySQL Question

How to filter query results of multiple database tables

I am trying to add a filter to a query which involves 3 different database tables within the single database.

I was able to initially create a complete list of the information I was trying to obtain and loop it into a table using the following code.

<?php
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number");
// $query = "SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number";
// $result = $wpdb->get_results($query);
?>

<?php foreach ( $result as $query ) {
?>

// Table Loop Here

<php
}
?>


However when trying to alter the code to what I have below I am not seeing any results. Where did I go wrong and how would I correct this?

<?php
global $wpdb;
$userid = $current_user->user_login;
$type = $wpdb->get_results( "SELECT type FROM dbtable1 WHERE Member = '$userid'"); //
// $membertype = "SELECT type FROM dbtable1 WHERE Member = '$userid'";
// $type = $wpdb->get_results($membertype);

$result = $wpdb->get_results("SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number");
// $query = "SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number";
// $result = $wpdb->get_results($query);
}
?>

<?php foreach ( $result as $query ) {
?>

// Table Loop Here

<?php
}
?>


Here is the loop. (dropped styling elements for simplicity)

<table>
<tr>
<th>Member Type</th>
<th>Date</th>
<th>Location</th>
<th>Description</th>
<th>Contact Info</th>
</tr>
<tr>
<td><?php echo $query->Type; ?></td>
<td><?php if($query->Date!=""){echo date('m/d/y', strtotime($query->Date)); } ?></td>
<td><?php echo $query->Location; ?></td>
<td><?php echo $query->Desc; ?></td>
<td><?php echo $query->Info; ?></td>
</tr>
</table>


I am using the same loop on both pages however the second code snippet is not populating anything on the frontend when I view the page where the first code snippet

In the first code snippet I am able to load all meetings and new meetings of all member types however the second code snippet which I have tried adding a extra sql query in order to filter down the results to a specific member type depending on the logged in user does not seem to produce any results even though I can see in mysql the results are there.

How can I edit the second code snippet in order to correctly filter the first snippets results for a specific "Type" of member.

Answer Source

OK so I have been able to successfully accomplish my goal and now have the filtered results populating on the page.

I have described the changes I had to make and the code I used below

In the end though a bit more advanced of a query the resulting statement is much cleaner as well using less lines of code which was a bonus.

I had to change from creating multiple separate queries and culminate them all into a single query using the "INNER JOIN" keyword.

From there it was as simple as connecting the correct records which had matching values within the various tables.

Here is the old code again for reference

 <?php
    global $wpdb;
    $result = $wpdb->get_results("SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number");
      // $query = "SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number";
      // $result = $wpdb->get_results($query);
  ?>

 <?php
    global $wpdb;
    $userid = $current_user->user_login;
    $type = $wpdb->get_results( "SELECT type FROM dbtable1 WHERE Member = '$userid'"); //
      // $membertype = "SELECT type FROM dbtable1 WHERE Member = '$userid'";
      // $type = $wpdb->get_results($membertype);

    $result = $wpdb->get_results("SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number");
       // $query = "SELECT * FROM meetings, new_meetings WHERE Type = '$type' ORDER BY code, Unit, Number";
       // $result = $wpdb->get_results($query);
    }
  ?>

  <?php foreach ( $result as $query ) {
  ?>

And here is the revised query.

<?php
global $wpdb;
$userid = $current_user->user_login;
$result = $wpdb->get_results("SELECT * FROM meetings as A inner join dbtable1 as B on A.Type = B.Type inner join new_meetings as C on C.Type = A.Type where B.Member = '$userid'");
 // $query = "SELECT * FROM meetings as A inner join dbtable1 as B on A.Type = B.Type inner join new_meetings as C on C.Type = A.Type where B.Member = '$userid'";
// $result = $wpdb->get_results($query);
  ?>

  <?php foreach ( $result as $query ) {
  ?>
 // Table Loop Here

  <?php
  }
  ?>

The result will pull only the specific rows where the current logged in members member type is present within the specific column we have stated within the query statement.

Hope this will be useful to others as it was for me researching it :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download