Sorting MYSQL Tag table(排序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标签表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:排序MYSQL标签表
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
