English 中文(简体)
如何显示“ 最受欢迎的” 标签
原标题:How to show the "most popular" tags

我有一个输入框, 用户在提问时可以输入标签。 他们用逗号分隔, 例如 :

test1, test2, test3, test4

我的 questions 表格结构是:

""https://i.sstatic.net/vrXnj.png" alt="此处输入图像描述"/ >

要显示问题标签, 我只需要使用 exlode (",, $qinfo [ tags ]) , 然后是 foreach 环 。

但我不知道我怎样才能在不同的“大众标签”页面上 展示最受欢迎到最不受欢迎的标签。

谢谢你的帮助!

最佳回答

您应该创建 tags questions_tags 表格。

tags
----
id
name

questions_tags
-------------
id
question_id
tag_id

并获得按流行程度分类的标签:

SELECT t.id, t.name, COUNT(qt.id) AS total
FROM tags t
LEFT JOIN questions_tags qt ON t.id=qt.tag_id
GROUP BY t.id, t.name
ORDER BY total DESC

但我建议您将 tags 字段保留在 questions 表格中。 使用它来缓存 。 例如: 如果您重新显示单个问题, 无需查询 tags questions_tags 表格 。

问题回答

您应该将标签放入其他表格并分开( 而不是用逗号分隔) 。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签