2011-11-22 45 views
-2

我有一个表的数据: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); 

感谢的捆绑提前!

周杰伦:-)

回答

1

简单的方法来获得科拉姆计数一个查询:

(select 'one' as col, one as item, count(one) as count from table_numbers group by one) UNION 
(select 'two', two, count(two) from table_numbers group by two) UNION 
(select 'three', three, count(three) from table_numbers group by three) UNION 
(select 'four', four, count(four) from table_numbers group by four) UNION 
(select 'five', five, count(five) from table_numbers group by five) UNION 
(select 'six', six, count(six) from table_numbers group by six) UNION 
ORDER BY col, count DESC 

这要复杂得多。如果你想在列汇总(例如3列):

SELECT tone.item as One, tone.count as `Count(one)`, ttwo.item as Two, ttwo.count as `Count(two)`, tthree.item as Three, tthree.count as `Count(three)` 
FROM 
    (SELECT @rownumtmp:[email protected]+1 as rownum 
    FROM 
    (SELECT DISTINCT col, count 
    FROM (
     (select 'one' as col, one as item, count(one) as count from table_numbers group by one) UNION 
     (select 'two', two, count(two) from table_numbers group by two) UNION 
     (select 'three', three, count(three) from table_numbers group by three)) tmp) tmp2, 
    (SELECT @rownumtmp:=0) r) tmp2 LEFT OUTER JOIN 
    (SELECT @rownum1:[email protected]+1 as rownum, item, count FROM (SELECT one as item, count(one) as count from table_numbers group by one ORDER BY count DESC, one) d, (SELECT @rownum1:=0) r) tone ON tmp2.rownum=tone.rownum LEFT OUTER JOIN 
    (SELECT @rownum2:[email protected]+1 as rownum, item, count FROM (SELECT two as item, count(two) as count from table_numbers group by two ORDER BY count DESC, two) d, (SELECT @rownum2:=0) r) ttwo ON tmp2.rownum=ttwo.rownum LEFT OUTER JOIN 
    (SELECT @rownum3:[email protected]+1 as rownum, item, count FROM (SELECT three as item, count(three) as count from table_numbers group by three ORDER BY count DESC, three) d, (SELECT @rownum3:=0) r) tthree ON tmp2.rownum=tthree.rownum 
WHERE tone.item IS NOT NULL OR ttwo.item IS NOT NULL OR tthree.item IS NOT NULL 

结果的以上查询将如下所示:

 
One Count(one) Two Count(two) Three Count(three) 
----------------------------------------------------- 
43 2   12 2   3  3    
3 1   1 1   2  2  
4 1   2 1   52  1   
11 1   31 1   null null    
12 1   32 1   null null     
------------------------------------------------------ 
+0

非常感谢帮助我,是的它非常接近我需要的,但它只显示三行。 – Jay

+0

我修复了第二个查询。对您的样本数据进行测试会返回您所需的数据。 – rogal111

+0

是的,这正是我需要的,伟大的工作。非常感谢Rogal。 – Jay

0

你碰巧这个意思?:

select one, two, three, four, five, six, count(1) 
from table_numbers group by one, two, three, four, five, six 
order by count(1) desc 

你试图寻找重复?

+0

是试图找到重复项。上面的查询不能正常工作,我需要找到列中最常出现的数字。 – Jay

0

我怀疑在单个查询中这样做的可能性! 执行时,查询只会遍历一次表格。在您的情况下,它需要计算多个列的 。

+0

那么你建议,写六个不同的查询来获得所需的数据? – Jay

+0

是啊!以我的知识,它是唯一简单易行的解决方案。其他将是一个复杂的子查询和连接,并将花费更重。..工作你的折衷公式.. – bmusical

0

您的意思是?

SELECT 
    one AS val, COUNT(*) AS c 
FROM ( 
    SELECT one FROM table_numbers UNION ALL 
    SELECT two FROM table_numbers UNION ALL 
    SELECT three FROM table_numbers UNION ALL 
    SELECT four FROM table_numbers UNION ALL 
    SELECT five FROM table_numbers UNION ALL 
    SELECT six FROM table_numbers 
) x GROUP BY one ORDER BY c DESC; 
+0

@Iqez,您的查询从所有表数据计数,我需要从每个列分开。 – Jay

+0

@Jay,每列的行数可能不相同。所以我不确定你想要什么。你能描述你想要的欲望结果吗? – lqez

+0

@Iqez,是的,请再次检查问题。我已根据我确切需要更新它。谢谢 – Jay

0

我看到可以完成的唯一方法是...将数据从表中导出为一个长整数的字符串。即平面文件并将数据作为一个字段导回到新表格。编写sql语句以将重复推断到新字段并进行总计数。直到导入的数据字段为空为止。希望有所帮助。