2011-11-22 46 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語句以將重複推斷到新字段並進行總計數。直到導入的數據字段爲空爲止。希望有所幫助。