MySQL 从表中选择不同的值,并按具有这些不同值的行数进行排序
我目前正在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的是
GROUP BY
和COUNT
。有关详细信息,请参阅:http://dev.mysql.com /doc/refman/5.1/en/counting-rows.html。
What you'll need is
GROUP BY
andCOUNT
.For more information, see: http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html.