MySQL:计数、分组依据和排序依据:整个表数据?

发布于 2024-12-18 00:29:04 字数 2281 浏览 2 评论 0原文

我有一个包含数据的表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素染倾城色 2024-12-25 00:29:04

在一个查询中获取列数的简单方法:

(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:=@rownumtmp+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:=@rownum1+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:=@rownum2+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:=@rownum3+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                
------------------------------------------------------

Easy way to get colum count in one query:

(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

It is much more complex If you want summary in columns (example for 3 columns):

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:=@rownumtmp+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:=@rownum1+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:=@rownum2+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:=@rownum3+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

Result of above query will be look like this:

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                
------------------------------------------------------
爱给你人给你 2024-12-25 00:29:04

你碰巧是这个意思吗?:

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

你想找到重复的吗?

Do you happen to mean this?:

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

Are you trying to find duplicates?

勿忘初心 2024-12-25 00:29:04

我怀疑在单个查询中执行此操作的可能性!
查询在执行时仅遍历表一次..在您的情况下它需要计数
不止一列。

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.

jJeQQOZ5 2024-12-25 00:29:04

你是这个意思吗?

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;

Did you mean this?

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;
辞别 2024-12-25 00:29:04

我认为可以做到的唯一方法是...将数据从表中导出为一长串整数。即一个平面文件并将数据作为一个字段导入回新表。编写 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文