简体   繁体   English

如何过滤多个数据库表的查询结果

[英]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. 我正在尝试向包含单个数据库内的3个不同数据库表的查询添加过滤器。

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. 在第一个代码段中,我能够加载所有成员类型的所有会议和新会议,但是我尝试添加第二个SQL查询的第二个代码段,以便根据登录的内容将结果筛选为特定的成员类型即使我可以在mysql中看到结果,用户似乎也不会产生任何结果。

How can I edit the second code snippet in order to correctly filter the first snippets results for a specific "Type" of member. 我如何编辑第二个代码段,以便正确过滤特定成员“类型”的第一个代码段结果。

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. 我不得不从创建多个单独的查询转变为使用“ INNER JOIN”关键字将它们最终合并为一个查询。

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 :) 希望这对其他人有用,就像对我研究它一样:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM