选择所有帖子及其类别和标签以显示在主页上

时间:2019-12-05 10:26:19

标签: php mysql pdo

我想选择要显示在主页上的所有帖子及其类别和标签,我也想选择其他查询中单个帖子的所有类别和标签以显示在帖子页面上,目前,但是我在foreach中使用3个查询来获取所有数据,但是我认为这是多余的,我想知道是否有可能在单个查询中获取所有数据,这是我的表的简化示例:

帖子

| id_post | title_post    | created_at          |
+---------+---------------+---------------------+
|       1 | Post Title  1 | 2019-11-26 06:40:03 |
|       2 | Post Title  2 | 2019-11-26 06:40:03 |
|       3 | Post Title  3 | 2019-11-26 06:40:03 |
+---------+---------------+---------------------+

POST_CATEGORIES

+---------+-------------+
| post_id | category_id |
+---------+-------------+
|       1 |           1 |
|       1 |           2 |
|       1 |           3 |
|       2 |           2 |
|       3 |           1 |
|       3 |           3 |
+---------+-------------+

类别

+--------+----------------+
| id_cat | title_category |
+--------+----------------+
|      1 | Cat Name 1     |
|      2 | Cat Name 2     |
|      3 | Cat Name 3     |
+--------+----------------+

POST_TAGS

+---------+-----------+
| post_id | tag_id    |
+---------+-----------+
|       1 |         1 |
|       1 |         2 |
|       1 |         3 |
|       2 |         4 |
|       2 |         5 |
|       2 |         6 |
|       3 |         6 |
+---------+-----------+

标签

+--------+-------------+
| id_tag | tag_title   |
+--------+-------------+
|      1 | TagName1    |
|      2 | TagName2    |
|      3 | TagName3    |
|      4 | TagName4    |
|      5 | TagName5    |
|      6 | TagName6    |
+--------+-------------+

因此要在主页上显示,我使用以下代码:

$sql = "SELECT * FROM posts WHERE active_post = 1 ORDER BY id_post $mode LIMIT $base, $max";

 <?php foreach ($posts as $key => $value) : ?>
  <article class="col-12 post">
    <div class="post__header py-4 px-0">
      <h2 class="font-weight-bold post__title">
        <a href="<?php echo $blog['domain'] . $value['url_post'] ?>"><?php echo $value['title_post'] ?></a>
      </h2>
      <div class="post__info">
        <ul class="mb-0 text-muted">
          <li>
            <span class="text-muted">
              <i class="far fa-calendar-alt icon"></i>
              <?php echo $value['created_at'] ?>
            </span>
          </li>
          <li>
            <span class="text-muted">
              <i class="far fa-folder icon"></i>
            </span>
            <?php

              $cat = BlogController::getCategories($value['id_post']);
              $c = '';
              if (!empty($cat)) {
                foreach ($cat as $k => $val) {
                  $c .= '<a class="text-muted" href="' . $blog['domain'] . 'category/' . $val["url_category"] . '">' . $val["title_category"] . '</a>, ';
                }
                echo substr($c, 0, -2);
              } else {
                echo '<a class="text-muted" href="not-category">Not Category</a>';
              }
              ?>
          </li>
          <li>
            <?php
              $tags = BlogController::getTags(null, $value['id_post']);
              $t = '';
              if (!empty($tags)) {
                foreach ($tags as $key => $val) {
                  $t .= '<a class="text-muted" href="' . $blog['domain'] . 'tags/' . $val["url_tags"] . '">' . $val["title_tag"] . '</a>, ';
                }
                echo '<span class="text-muted">
                        <i class="fas fa-hashtag icon"></i>
                      </span>';
                echo substr($t, 0, -2);
              }
              ?>
          </li>
        </ul>
      </div>
    </div>
    <figure class="mb-0">
      <img src="<?php echo $blog['domain'] . substr($value['img_post'], 22); ?>" alt="<?php echo $value['title_post'] ?>" class="img-fluid post__image" loading="lazy" />
    </figure>
    <div class="post__body">
      <p class="post__text">
        <?php echo $value['desc_post'] ?>
      </p>
      <a href="<?php echo  $blog['domain'] . $value['url_post'] ?>" class="post__link color-link">Read more... <span>→</span></a>
    </div>
  </article>
<?php endforeach; ?>

BlogController将信息发送到BlogModel并执行以下查询的类别和标签:

$sql = "SELECT p.post_id, cat.title_category FROM post_categories as p INNER JOIN categories as cat ON p.category_id = cat.id_cat WHERE p.post_id = :id";

$sql = "SELECT p.post_id, tag.title_tag FROM post_tags as p INNER JOIN tags as tag ON p.tag_id = tag.id_tag WHERE p.post_id = :id";

所以我想做一个查询以获取我尝试执行的所有信息:

$sql = "SELECT p.id_post, p.title_post, p.created_at, cat.title_category, tag.title_tag FROM posts as p INNER JOIN post_categories AS pc INNER JOIN categories AS cat INNER JOIN tags as tag INNER JOIN post_tags AS pt ON pc.post_id = p.id_post WHERE pc.category_id = cat.id_cat AND pt.post_id = tag.id_tag LIMIT 0, 10";

这是结果(结果限制为10个,仅用于不打印大数据):

+---------+---------------+---------------------+------------------+-----------------+
| id_post | title_post    | created_at         | title_category   | title_tag       |
+---------+---------------+---------------------+------------------+-----------------+
|       1 | Title Post 1 | 2019-11-26 06:40:03 | Cat Name 1       | TagName1        |
|       1 | Title Post 1 | 2019-11-26 06:40:03 | Cat Name 2       | TagName1        |
|       1 | Title Post 1 | 2019-11-26 06:40:03 | Cat Name 3       | TagName1        |
|       2 | Title Post 2 | 2019-11-26 06:40:03 | Cat Name 1       | TagName1        |
|       2 | Title Post 2 | 2019-11-26 06:40:03 | Cat Name 2       | TagName1        |
|       2 | Title Post 2 | 2019-11-26 06:40:03 | Cat Name 3       | TagName1        |
|       3 | Title Post 3 | 2019-11-26 06:40:03 | Cat Name 1       | TagName1        |
|       3 | Title Post 3 | 2019-11-26 06:40:03 | Cat Name 2       | TagName1        |
|       3 | Title Post 3 | 2019-11-26 06:40:03 | Cat Name 3       | TagName1        |
|       4 | Title Post 4 | 2019-11-26 06:40:03 | Cat Name 1       | TagName1        |
+---------+---------------+---------------------+------------------+-----------------+

Here,您可以从我的主页上查看屏幕截图,如果有人可以帮助我实现这一目标,我非常感谢。

这就是我想要得到的:

+---------+--------------+---------------------+------------------------------------+-----------------------------+
| id_post | title_post   | created_at          | title_categories                   | title_tags                  |
+---------+--------------+---------------------+------------------------------------+-----------------------------+
| 1       | Post title 1 | 2019-12-05 05:44:47 | Cat Name 1, Cat Name 2, Cat Name 3 | TagName1, TagName2,TagName3 |
+---------+--------------+---------------------+------------------------------------+-----------------------------+
| 2       | Post title 2 | 2019-12-05 05:44:47 | Cat Name 1, Cat Name 2             | TagName4                    |
+---------+--------------+---------------------+------------------------------------+-----------------------------+
| 3       | Post title 3 | 2019-12-05 05:44:47 | Cat Name 1                         | TagName3, TagName5,TagName6 |
+---------+--------------+---------------------+------------------------------------+-----------------------------+

预先感谢

1 个答案:

答案 0 :(得分:1)

这是针对您的目的的查询,该查询通过联接5个正在起作用的表而起作用。

select 
    p.id_post,
    p.title_post,
    p.created_at,
    group_concat(distinct c.title_category order by c.title_category) title_category,
    group_concat(distinct t.tag_title order by t.tag_title) tag_title 
from posts p
inner join post_categories pc on pc.post_id = p.id_post
inner join categories c on c.id_cat = pc.category_id
inner join post_tags pt on pt.post_id = p.id_post
inner join tags t on t.id_tag = pt.tag_id
group by
    p.id_post,
    p.title_post,
    p.created_at
order by ??
limit 10

注意:为使limit子句有意义,您需要定义order by条件。我在查询中添加了该子句,但带有问号,请用相关的列名替换它。