简体   繁体   English

MySQL 跨行和多列对 select 中的值进行评分

[英]MySQL Scoring of values from select across rows and multiple columns

So I have a scheme like the following:所以我有如下方案:

| phone1        | phone2      | phone3      | phone4        | phone5      | phone6        |
| ------------- | ----------- | ----------- | ------------- | ----------- | ------------- |
|  447488575023 | 07488575023 | 07397005898 |  441535930275 | 01535930275 |  07397005898  |
| 01606592390   | 07968233423 | 07968233423 | 01606592490   | 07968233423 |  441606592490 |
| 01606592390   | 07968233423 | 07968233423 | 01606592490   | 07968233423 |  441606592490 |
|  447544737701 | 07544737701 | 07544737701 | 07544737701   | 07587989521 |  447402204547 |
|  447383626815 | 07383626815 | 07383626815 | 07383626815   | 07508889595 |  447412987535 |
|  441752313756 | 01752313756 | 07958697492 | 01752313756   | 07746624841 |  447958437692 |
|  447784301122 | 07784301122 | 07784301122 | 07784301122   | 07732274851 |  447979879900 |

What I want to achieve is to get the top 6 unique occurring numbers in order of the count across all rows and columns.我想要实现的是按照所有行和列的计数顺序获得前 6 个唯一出现的数字。

You need to unpivot.你需要取消枢轴。 In MySQL, this is typically done using union all :在 MySQL 中,这通常是使用union all完成的:

select phone, count(*)
from ((select phone1 as phone from t) union all
      (select phone2 as phone from t) union all
      (select phone3 as phone from t) union all
      (select phone4 as phone from t) union all
      (select phone5 as phone from t) union all
      (select phone6 as phone from t) 
     ) p
group by phone
order by count(*) desc
limit 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM