MySQL跨行和多列对select中的值进行评分
因此,我有一个类似以下的计划:MySQL跨行和多列对select中的值进行评分,mysql,sql,database,Mysql,Sql,Database,因此,我有一个类似以下的计划: | phone1 | phone2 | phone3 | phone4 | phone5 | phone6 | | ------------- | ----------- | ----------- | ------------- | ----------- | ------------- | | 447488575023 | 07488575023 | 07397005898 | 441
| 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 |
我想要实现的是按照所有行和列的计数顺序获取前6个唯一出现的数字。您需要取消PIVOT。在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;
数据库表不是电子表格。考虑修改你的模式。@ Strawberry,我没有写模式,相信我,我希望我这样做。