计算帖子数量使用每个标签

发布于 2025-02-13 05:02:35 字数 2165 浏览 1 评论 0原文

因此,我正在研究一个自定义博客系统,每篇文章中,我都会显示与该帖子相关的标签列表。我想在每个标签中旁边的每个标签旁边都有一个数量的数量,即其他每个标签使用了多少帖子,这就是我在弄清楚的问题。

在这里,我有一张帖子,标签,然后是post_tags的3个表,用于分配哪些帖子具有标签。

mysql> DESC post; 
+----------+--------------+------+-----+---------+----------------+ 
| Field    | Type         | Null | Key | Default | Extra          | 
+----------+--------------+------+-----+---------+----------------+ 
| id       | mediumint(9) | NO   | PRI | NULL    | auto_increment | 
| contents | longtext     | NO   |     | NULL    |                | 
+----------+--------------+------+-----+---------+----------------+

mysql> DESC tags; 
+-------+--------------+------+-----+---------+----------------+ 
| Field | Type         | Null | Key | Default | Extra          | 
+-------+--------------+------+-----+---------+----------------+ 
| id    | mediumint(9) | NO   | PRI | NULL    | auto_increment | 
| tag   | varchar(255) | NO   |     | NULL    |                | 
+-------+--------------+------+-----+---------+----------------+ 

mysql> DESC post_tags; 
+---------+--------------+------+-----+---------+----------------+ 
| Field   | Type         | Null | Key | Default | Extra          | 
+---------+--------------+------+-----+---------+----------------+ 
| id      | mediumint(9) | NO   | PRI | NULL    | auto_increment | 
| post_id | mediumint(9) | NO   |     | NULL    |                | 
| tag_id  | mediumint(9) | NO   |     | NULL    |                | 
+---------+--------------+------+-----+---------+----------------+

这是我用来提取帖子标签列表的代码

$stmt = $con->prepare('SELECT * FROM tags LEFT JOIN post_tags ON post_tags.tag_id = tags.id JOIN post ON post.id = post_tags.post_id WHERE post.id = ?');
$stmt->bind_param('i', $_GET['id']);
$stmt->execute();
$tags = $stmt->get_result();

,以及如何显示它们

<?php while ($tag = $tags->fetch_assoc()) : ?>
<span class="tags"><a href="tags.php?<?= $tag['tag'] ?>" class="tag tag-35762 "><span class="name"><?= $tag['tag'] ?></span><span class="count">1</span></a>
<? endwhile ?>

So I am working on a custom blog system where on each post I show the list of tags associated with that post. Next to each one of the the tags I want to show a count of how many other posts use each tag, and that is what I am having issue figuring out.

Here the the 3 tables I have one for the posts, tags, and then post_tags for assigning which posts have what tags.

mysql> DESC post; 
+----------+--------------+------+-----+---------+----------------+ 
| Field    | Type         | Null | Key | Default | Extra          | 
+----------+--------------+------+-----+---------+----------------+ 
| id       | mediumint(9) | NO   | PRI | NULL    | auto_increment | 
| contents | longtext     | NO   |     | NULL    |                | 
+----------+--------------+------+-----+---------+----------------+

mysql> DESC tags; 
+-------+--------------+------+-----+---------+----------------+ 
| Field | Type         | Null | Key | Default | Extra          | 
+-------+--------------+------+-----+---------+----------------+ 
| id    | mediumint(9) | NO   | PRI | NULL    | auto_increment | 
| tag   | varchar(255) | NO   |     | NULL    |                | 
+-------+--------------+------+-----+---------+----------------+ 

mysql> DESC post_tags; 
+---------+--------------+------+-----+---------+----------------+ 
| Field   | Type         | Null | Key | Default | Extra          | 
+---------+--------------+------+-----+---------+----------------+ 
| id      | mediumint(9) | NO   | PRI | NULL    | auto_increment | 
| post_id | mediumint(9) | NO   |     | NULL    |                | 
| tag_id  | mediumint(9) | NO   |     | NULL    |                | 
+---------+--------------+------+-----+---------+----------------+

Here is the code I use to pull the list of tags for the post

$stmt = $con->prepare('SELECT * FROM tags LEFT JOIN post_tags ON post_tags.tag_id = tags.id JOIN post ON post.id = post_tags.post_id WHERE post.id = ?');
$stmt->bind_param('i', $_GET['id']);
$stmt->execute();
$tags = $stmt->get_result();

and how I display them

<?php while ($tag = $tags->fetch_assoc()) : ?>
<span class="tags"><a href="tags.php?<?= $tag['tag'] ?>" class="tag tag-35762 "><span class="name"><?= $tag['tag'] ?></span><span class="count">1</span></a>
<? endwhile ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぶ宁プ宁ぶ 2025-02-20 05:02:35

这样的东西?

SELECT t.*, IF(post_count is null,0,post_count) as post_count
FROM tags AS t 
LEFT JOIN(SELECT COUNT(*) as post_count,tag_id FROM post_tags GROUP BY tag_id) as pcount 
     ON pcount.tag_id = t.id

或者

SELECT t.*, count(pt.id) as post_count
FROM tags AS t 
LEFT JOIN post_tags pt ON pt.tag_id = t.id
GROUP BY t.id

Something like this?

SELECT t.*, IF(post_count is null,0,post_count) as post_count
FROM tags AS t 
LEFT JOIN(SELECT COUNT(*) as post_count,tag_id FROM post_tags GROUP BY tag_id) as pcount 
     ON pcount.tag_id = t.id

OR

SELECT t.*, count(pt.id) as post_count
FROM tags AS t 
LEFT JOIN post_tags pt ON pt.tag_id = t.id
GROUP BY t.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文