2017-04-23 38 views
0

我有一個數據庫,我通過PHP和mysqli訪問。我需要獲得前5個最重複的值。PHP SQL查找前5個最重複值

例如, 數據庫將具有在它 1,1,2,2,2,4,6,5,5,5,5,

數據庫應該連續

打印出

:5,2,1,4,6

+1

「對於例如,數據庫將在其中包含1,1,2,2,4,6,5,5,5,5「......這是什麼?數據庫不包含事物列表;它們包含包含列和行的表。單個表中的單個列的值是多少? – Chris

+0

你是否對這些數字「1,1,2,2,2,4,6,5,5,5,5」進行排列,或者它們是字符串? –

回答

0

Try this code snippet here

<?php 
ini_set('display_errors', 1); 
$array=array(1, 1, 2, 2, 2, 4, 6, 5, 5, 5, 5); 
$data=array_count_values($array);//counting the no. of values in an array 
arsort($data);//sorting array in descending order 
print_r(array_keys($data));//getting the key of array which is the desired output 
2

你是問什麼的SQL查詢將得到最occurances到最低?

這個位置將由號碼,然後一羣來自最occurances責令其最低

SELECT number , count(number) as 'occurrences' 
FROM my_table 
GROUP BY number 
ORDER BY count(number) DESC 
0

如果你需要的結果作爲一個字符串(連續):

SELECT STUFF((SELECT ',' + convert(varchar,my_number) FROM my_table group by 
my_number ORDER BY count(my_number) desc for XML PATH('')), 1, 1, '')