我有一个表的数据: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 -------------------------------------------------
,所以我知道如何通过与秩序一列数,组,就像这样:
select one, count(one) from table_numbers group by one order by count(one) 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);
感谢的捆绑提前!
周杰伦:-)
非常感谢帮助我,是的它非常接近我需要的,但它只显示三行。 – Jay
我修复了第二个查询。对您的样本数据进行测试会返回您所需的数据。 – rogal111
是的,这正是我需要的,伟大的工作。非常感谢Rogal。 – Jay