在一个div中使用两个表

时间:2016-08-12 18:10:04

标签: php mysql join count

我有一个表作为blog_posts

+---------+-----------+
| postID | categoryID |
+========+============+
| 1      |  1         | 
+--------+------------+
| 2      |  1         |
+--------+------------+
| 3      |  2         | 
+--------+------------+  
| 4      |  4         | 
+--------+------------+

另一个是blog_category

+---------+-----------+
| CategoryID | catName |
+========+============+
| 1      |  cricket   | 
+--------+------------+
| 2      |  sports    |
+--------+------------+
| 3      |  football  | 
+--------+------------+  
| 4      |  tennis    | 
+--------+------------+

现在我想显示类别以及类别上有多少帖子。像categoryID 1那样在postID 1和2中,所以,它会显示板球,帖子2.现在我只提取类别,这很容易:P

     <ul>
       <?php
       if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($conn,"SELECT * FROM blog_category ");
    while($row = mysqli_fetch_array($result)) {



    ?>
     <li>
            <a class="f-categories-filter_name" href="blog-cat.php?id=<?php echo $row['categoryID'];?>"><i class="fa fa-plus"></i><?php echo $row['categoryName'];?></a>
<span class="b-categories-filter_count f-categories-filter_count">**I want to display here number of posts of that category**</span>
        </li>
        <?php } ?>
    </ul>

3 个答案:

答案 0 :(得分:1)

您必须更改SQL查询才能获取数据。有很多方法可以加入它。我写了一个。

SQL

SELECT blog_category.* , count(*) as total_post 
FROM blog_category INNER JOIN blog_posts 
ON blog_category.CategoryID = blog_posts.categoryID 
GROUP BY blog_category.Category

完整代码

<ul><?php

  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $result = mysqli_query($conn,"SELECT * FROM blog_category ");

  while($row = mysqli_fetch_array($result)) {

    ?><li>
      <a class="f-categories-filter_name" href="blog-cat.php?id=<?php echo $row['categoryID'];?>"><i class="fa fa-plus"></i><?php echo $row['categoryName'];?></a>
      <span class="b-categories-filter_count f-categories-filter_count"><?php echo $row['total_post']; ?></span>
    </li><?php

  }

?></ul>

了解更多信息

  

SQL join:http://www.w3schools.com/sql/sql_join_inner.asp
  SQL Count:http://www.w3schools.com/sql/sql_func_count.asp

答案 1 :(得分:0)

Yuo可以使用查询直接获取数据

完整列表

select blog_category.catName   group_concat(blog_posts.postID)
from blog_posts
inner join blog_category on blog_category.CategoryID = blog_posts.CategoryID 
group by blog_category.catName ;

或单一类别

select blog_category.catName   group_concat(blog_posts.postID)
from blog_posts
inner join blog_category on blog_category.CategoryID = blog_posts.CategoryID 
where blog_category.CategoryID = 1
group by blog_category.catName ;

答案 2 :(得分:0)

要获取每个类别中的帖子数,您需要使用以下SQL查询:

SELECT 
   blog_category.*, 
   COUNT(blog_posts.postID) AS cnt
FROM 
   blog_category
INNER JOIN
   blog_posts
ON
   blog_posts.categoryID=blog_category.categoryID
GROUP BY
   blog_category.categoryID;

所以它会是这样的:

 <ul>
 <?php
   if (mysqli_connect_errno()) {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   //here we use a modified query
   $result = mysqli_query($conn, "SELECT 
   blog_category.*, 
   COUNT(blog_posts.postID) AS cnt
FROM 
   blog_category
INNER JOIN
   blog_posts
ON
   blog_posts.categoryID=blog_category.categoryID
GROUP BY
   blog_category.categoryID;");
       while($row = mysqli_fetch_array($result)) {
    ?>
     <li>
            <a class="f-categories-filter_name" href="blog-cat.php?id=<?php echo $row['categoryID'];?>"><i class="fa fa-plus"></i><?php echo $row['categoryName'];?></a>
<span class="b-categories-filter_count f-categories-filter_count"><?php echo $row['cnt'];?></span>
        </li>
        <?php } ?>
    </ul>