排序MYSQL标签表 [英] Sorting MYSQL Tag table

查看:23
本文介绍了排序MYSQL标签表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否有可能获得前 10 个 COUNT 结果并按 COUNT 和字母顺序排序?

just wondering if it is possible to get the top 10 COUNT results and ordering by COUNT and alphabetically?

我有以下表格,

tags
-------
id | title

.

tagged
------
tag_id | post_id

以及以下 SQL 查询

And the following SQL query

SELECT tag.*, COUNT(td.tag_ID) AS tagcount
FROM Tagged td
LEFT JOIN Tags tag ON td.tag_ID = tag.tag_ID
GROUP BY td.tag_ID
ORDER BY tagcount DESC, tag.tag_Title ASC

有什么想法吗?

提前致谢

对不起,如果我没有正确解释.

Sorry if I didnt explain it properly.

查询有效,我没有添加 LIMIT 10,因为想先查看整个结果集.

The query works and I didnt add LIMIT 10 due to wanting to see the entire result set first.

我的查询有效,但是在以下示例结果中

The query I have works, however at the following example result

tag_ID  tag_Title  tagcount
1          Science  3
3          Chemistry 2
4          Misc      1
5          Maths       1
2          Sport       1

不过,我希望化学超越科学.

I would want Chemistry to come above Science though.

即前十名最高计数......按字母顺序排序

i.e. top ten highest counts.. sorted alphabetically

谢谢你们……丹尼尔和雪橇.

Thanks to you both.. Daniel and Sled.

这是一个工作示例

( 
   SELECT     t.*, COUNT(*) AS tagcount
   FROM       tagged td
   LEFT JOIN  tags t ON (t.id = td.tag_id)
   GROUP BY   td.tag_id
   ORDER BY   tagcount DESC, t.title ASC
   LIMIT      3
) ORDER BY title ASC;

推荐答案

更新:

进一步了解下面的新评论:

Further to the new comment below:

( 
   SELECT     t.*, COUNT(*) AS tagcount
   FROM       tagged td
   LEFT JOIN  tags t ON (t.id = td.tag_id)
   GROUP BY   td.tag_id
   ORDER BY   tagcount DESC, t.title ASC
   LIMIT      3
) ORDER BY title ASC;

结果:

+------+------------+----------+
| id   | title      | tagcount |
+------+------------+----------+
|    3 | javascript |        2 |
|    1 | mysql      |        2 |
|    2 | php        |        3 |
+------+------------+----------+
3 rows in set (0.00 sec)

只需将 LIMIT 3 更改为 LIMIT 10 即可获得前 10 名而不是前 3 名.

Simply change the LIMIT 3 to LIMIT 10 to get the top 10 instead of the top 3.

上一个答案:

为什么不在查询中添加 LIMIT 10 ?

Why don't you add a LIMIT 10 to your query?

SELECT     t.*, COUNT(*) AS tagcount
FROM       tagged td
LEFT JOIN  tags t ON (t.id = td.tag_id)
GROUP BY   td.tag_id
ORDER BY   tagcount DESC, t.title ASC
LIMIT      10;

测试用例:

CREATE TABLE tags (id int, title varchar(20));
CREATE TABLE tagged (tag_id int, post_id int);

INSERT INTO tags VALUES (1, 'mysql');
INSERT INTO tags VALUES (2, 'php');
INSERT INTO tags VALUES (3, 'javascript');
INSERT INTO tags VALUES (4, 'c');

INSERT INTO tagged VALUES (1, 1);
INSERT INTO tagged VALUES (2, 1);
INSERT INTO tagged VALUES (1, 2);
INSERT INTO tagged VALUES (2, 2);
INSERT INTO tagged VALUES (3, 3);
INSERT INTO tagged VALUES (2, 4);
INSERT INTO tagged VALUES (3, 4);
INSERT INTO tagged VALUES (4, 5);

结果(使用LIMIT 3):

+------+------------+----------+
| id   | title      | tagcount |
+------+------------+----------+
|    2 | php        |        3 |
|    3 | javascript |        2 |
|    1 | mysql      |        2 |
+------+------------+----------+
3 rows in set (0.00 sec)

注意 [c] 标签是如何从前 3 个结果中掉出来的,并且在出现平局的情况下,行按字母顺序排列.

Note how the [c] tag fell out of the top 3 results, and rows are ordered alphabetically in case of a tie.

这篇关于排序MYSQL标签表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆