将帖子计为PHP和MYSQL类别

时间:2022-09-25 16:43:08

I've this code...

我有这个代码......

$lesson = mysql_query("SELECT * FROM categories WHERE gr='exrcs' ORDER BY title");
$flesson = mysql_fetch_array($lesson);
do {
    printf("<a href='exrcs?cat=%s'>%s</a>",$flesson['id'],$flesson['title']);
   } while ($flesson = mysql_fetch_array($lesson));

This code will be "HTML, CSS, PHP" and other...

这段代码将是“HTML,CSS,PHP”等...

But I want this "HTML (2), CSS (14), PHP(8)" and other...

但我想要这个“HTML(2),CSS(14),PHP(8)”等......

Here (2),(14),(8) amount posts on categories

这里(2),(14),(8)类别的数额

3 个解决方案

#1


1  

Update your query:

更新您的查询:

SELECT title, count(id) total FROM categories WHERE gr='exrcs' group by title ORDER BY title

or

SELECT concat(title,' (', count(id) ,')') as title FROM categories WHERE gr='exrcs' group by title ORDER BY title

#2


0  

Use count and group by

使用计数和分组

"SELECT cat as title , count(*)  FROM categories 
WHERE gr='exrcs' group by title  ORDER BY title"

#3


0  

"SELECT CONCAT(C.title,'(',COUNT(*),')') FROM categories C INNER JOIN post P ON C.ID= P.cat_id GROUP BY c.title"

#1


1  

Update your query:

更新您的查询:

SELECT title, count(id) total FROM categories WHERE gr='exrcs' group by title ORDER BY title

or

SELECT concat(title,' (', count(id) ,')') as title FROM categories WHERE gr='exrcs' group by title ORDER BY title

#2


0  

Use count and group by

使用计数和分组

"SELECT cat as title , count(*)  FROM categories 
WHERE gr='exrcs' group by title  ORDER BY title"

#3


0  

"SELECT CONCAT(C.title,'(',COUNT(*),')') FROM categories C INNER JOIN post P ON C.ID= P.cat_id GROUP BY c.title"