MySQL:计数、分组依据和排序依据:整个表数据?
我有一个包含数据的表:
id one two three four five six ------------------------------------------------ 1 12 32 2 5 34 13 2 43 12 3 33 22 17 3 11 31 3 15 13 13 4 43 12 52 73 29 19 5 3 2 2 3 9 9 6 4 1 3 7 2 19 -------------------------------------------------
所以我知道如何按一列进行计数、分组和排序,如下所示:
<代码> 选择一个,数(一) 来自表编号 以一为一组 按计数(一) desc 排序
上面的查询将为我们提供:
one count(one) ----------------- 43 2 3 1 4 1 11 1 12 1 -----------------
那么我如何在一个查询中获取所有列的数据?
像这样:
One Count(one) Two Count(two) Three Count(three) Four Count(four) --------------------------------------------------------------------------- 43 2 12 2 3 3 73 1 3 1 1 1 2 2 3 1 4 1 2 1 52 1 5 1 11 1 31 1 null null 7 1 12 1 32 1 null null 15 1 null null null null null null 33 1 ---------------------------------------------------------------------------
现在有什么方法可以在一个 sql 查询中完成它吗?可能正在使用连接或内联视图或其他什么?或者这在单个查询中可能吗?
[更新]我想计算每列中的重复值并按降序排序。
[更新] 如果您想使用表数据:
CREATE TABLE IF NOT EXISTS `table_numbers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `one` int(2) NOT NULL, `two` int(2) NOT NULL, `three` int(2) NOT NULL, `four` int(2) NOT NULL, `five` int(2) NOT NULL, `six` int(2) NOT NULL, PRIMARY KEY (`id`), KEY `col_one` (`one`), KEY `col_two` (`two`), KEY `col_three` (`three`), KEY `col_four` (`four`), KEY `col_five` (`five`), KEY `col_six` (`six`) ) ; INSERT INTO `table_numbers` (`id`, `one`, `two`, `three`, `four`, `five`, `six`) VALUES (1, 12, 32, 2, 5, 34, 13),(2, 43, 12, 3, 33, 22, 17),(3, 11, 31, 3, 15, 13, 13), (4, 43, 12, 52, 73, 29, 19),(5, 3, 2, 2, 3, 9, 9),(6, 4, 1, 3, 7, 2, 19);
提前致谢!
杰伊 :-)
I have a table with data :
id one two three four five six ------------------------------------------------ 1 12 32 2 5 34 13 2 43 12 3 33 22 17 3 11 31 3 15 13 13 4 43 12 52 73 29 19 5 3 2 2 3 9 9 6 4 1 3 7 2 19 -------------------------------------------------
So i know how to count, group by and order by one column , like this :
select one, count(one)
from table_numbers
group by one
order by count(one) desc
Above query will give us :
one count(one) ----------------- 43 2 3 1 4 1 11 1 12 1 -----------------
So how can i get data like above for all columns in one single query ?
Like this :
One Count(one) Two Count(two) Three Count(three) Four Count(four) --------------------------------------------------------------------------- 43 2 12 2 3 3 73 1 3 1 1 1 2 2 3 1 4 1 2 1 52 1 5 1 11 1 31 1 null null 7 1 12 1 32 1 null null 15 1 null null null null null null 33 1 ---------------------------------------------------------------------------
Now is there any way to do it in one single sql query ? May be using joins or inline views or anything else? Or Is this possible in single query ?
[update] I want to count duplicate values from every column and sort it in descending order.
[UPDATE] In case you want to use table data :
CREATE TABLE IF NOT EXISTS `table_numbers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `one` int(2) NOT NULL, `two` int(2) NOT NULL, `three` int(2) NOT NULL, `four` int(2) NOT NULL, `five` int(2) NOT NULL, `six` int(2) NOT NULL, PRIMARY KEY (`id`), KEY `col_one` (`one`), KEY `col_two` (`two`), KEY `col_three` (`three`), KEY `col_four` (`four`), KEY `col_five` (`five`), KEY `col_six` (`six`) ) ; INSERT INTO `table_numbers` (`id`, `one`, `two`, `three`, `four`, `five`, `six`) VALUES (1, 12, 32, 2, 5, 34, 13),(2, 43, 12, 3, 33, 22, 17),(3, 11, 31, 3, 15, 13, 13), (4, 43, 12, 52, 73, 29, 19),(5, 3, 2, 2, 3, 9, 9),(6, 4, 1, 3, 7, 2, 19);
Bundle of thanks in advance !
Jay :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在一个查询中获取列数的简单方法:
它要复杂得多如果您想在列中进行汇总(例如 3 列):
上述查询的结果将如下所示:
Easy way to get colum count in one query:
It is much more complex If you want summary in columns (example for 3 columns):
Result of above query will be look like this:
你碰巧是这个意思吗?:
你想找到重复的吗?
Do you happen to mean this?:
Are you trying to find duplicates?
我怀疑在单个查询中执行此操作的可能性!
查询在执行时仅遍历表一次..在您的情况下它需要计数
不止一列。
I doubt the possibility of doing this in single query!!
A query traverses the table only once when executed.. in your case it need to count
more than one columns.
你是这个意思吗?
Did you mean this?
我认为可以做到的唯一方法是...将数据从表中导出为一长串整数。即一个平面文件并将数据作为一个字段导入回新表。编写 sql 语句将重复项外推到新字段并进行总计。执行同样的操作,直到字段中导入的数据为空。希望有帮助。
The only way I see that can be done is...export your data from the table as one long string of integer. i.e. a flat file and import the data back to a new table as one field. Write sql statement to extrapolate the duplicate to a new field and do a total count. Do the same until the imported data in field is empty. Hope that help.