MySQL 从表中选择不同的值,并按具有这些不同值的行数进行排序

发布于 2024-11-17 15:11:15 字数 484 浏览 2 评论 0原文

我目前正在研究 MySQL 查询,但我遇到了一些麻烦,正在寻求帮助。我已经为此苦苦挣扎了一段时间,并从互联网和不同的编码论坛中搜索了解决方案,但是,唉,仍然一无所获。

基本上,我有一个 MySQL 表,如下所示:

Table name: 'data'
'id' SMALLINT(5) auto_increment
'value_1' SMALLINT(5)
'value_2' SMALLINT(5)

“value_2”列有重复值,因此,我需要找到不同的值来处理结果。所以我创建了一个 MySQL 查询来做到这一点。

SELECT DISTINCT value_1 FROM data WHERE value_2!="0"

我遇到的问题是,我想对刚刚获得的结果进行排序,不是按 id 也不是按任何其他字段,而是按该列中具有某些不同值的行数(“value_1”)。

有人可以帮助我吗?

I'm currently working on a MySQL query, but I've been having a bit of trouble with it and am looking for help. I have been struggling with it for a while now and have searched for the solution from the Internet and from different coding forums, but, alas, still nothing.

Basically, I have a MySQL table as follows:

Table name: 'data'
'id' SMALLINT(5) auto_increment
'value_1' SMALLINT(5)
'value_2' SMALLINT(5)

The column 'value_2' has duplicate values and, so, I need to find the distinct values to process the results. So I created a MySQL query to do that.

SELECT DISTINCT value_1 FROM data WHERE value_2!="0"

The problem I'm having is that I want to order the results I just had by not the id nor by any other field, but by the number of rows that had those certain distinct values in that column ('value_1').

Could anyone please assist me?

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

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

发布评论

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

评论(2

囍孤女 2024-11-24 15:11:15

您需要的是 GROUP BYCOUNT

SELECT `value_1`, COUNT(`value_1`) AS `count` FROM `data` WHERE `value_2` != '0' GROUP BY `value_1` ORDER BY `count`;

有关详细信息,请参阅:http://dev.mysql.com /doc/refman/5.1/en/counting-rows.html

What you'll need is GROUP BY and COUNT.

SELECT `value_1`, COUNT(`value_1`) AS `count` FROM `data` WHERE `value_2` != '0' GROUP BY `value_1` ORDER BY `count`;

For more information, see: http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html.

绝影如岚 2024-11-24 15:11:15
SELECT DISTINCT value_1, COUNT(*) FROM data GROUP BY value_1 ORDER BY 2 DESC;
SELECT DISTINCT value_1, COUNT(*) FROM data GROUP BY value_1 ORDER BY 2 DESC;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文