[英]Select all posts and their categories and tags to display in main page
I want to Select all posts and their categories and tags to display in main page, also I want to select all categories and tags for a single post in other query to display on post-page, at this moment I have done, but I'm using 3 querys inside a foreach to get all data, but I think it was redundat and I wish to Know if it is possible to take all data in a single query, this is an reduced example of my tables:我想选择所有帖子及其类别和标签以显示在主页上,我还想选择其他查询中单个帖子的所有类别和标签以显示在帖子页面上,此时我已经完成了,但是我我在 foreach 中使用 3 个查询来获取所有数据,但我认为它是冗余的,我想知道是否可以在单个查询中获取所有数据,这是我的表的简化示例:
POSTS帖子
| 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_CATEGORIES
+---------+-------------+
| post_id | category_id |
+---------+-------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 3 | 1 |
| 3 | 3 |
+---------+-------------+
CATEGORIES类别
+--------+----------------+
| id_cat | title_category |
+--------+----------------+
| 1 | Cat Name 1 |
| 2 | Cat Name 2 |
| 3 | Cat Name 3 |
+--------+----------------+
POST_TAGS POST_TAGS
+---------+-----------+
| post_id | tag_id |
+---------+-----------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
| 2 | 5 |
| 2 | 6 |
| 3 | 6 |
+---------+-----------+
TAGS标签
+--------+-------------+
| id_tag | tag_title |
+--------+-------------+
| 1 | TagName1 |
| 2 | TagName2 |
| 3 | TagName3 |
| 4 | TagName4 |
| 5 | TagName5 |
| 6 | TagName6 |
+--------+-------------+
So to display in main page I use this:所以为了在主页面上显示我使用这个:
$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 send the info to BlogModel and excecute this querys for categories and tags: 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";
So I would like to do a single query to get all info I try to do this:所以我想做一个查询来获取我尝试这样做的所有信息:
$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";
And here are the results(RESULTS LIMITED TO 10 just for don't print a big data):以下是结果(结果仅限于 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 you can see an screenshot from my main page, If anyone can help me to reach this I appreciate it so much thanks.在这里你可以看到我主页的截图,如果有人能帮助我达到这个,我非常感谢。
This is what I would like to get:这就是我想得到的:
+---------+--------------+---------------------+------------------------------------+-----------------------------+
| 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 |
+---------+--------------+---------------------+------------------------------------+-----------------------------+
Thanks in advance提前致谢
Here is query for your purpose, which works by joining the 5 tables that come into play.这是针对您的目的的查询,它通过加入起作用的 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
Note: for the limit
clause to make sense, you would need to define order by
criterias.注意:要使
limit
子句有意义,您需要order by
条件定义order by
。 I added that clause to the query but with question marks, please replace that with the relevant column names.我将该子句添加到查询中,但带有问号,请将其替换为相关的列名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.