2016-02-16 7 views
5

miei tag tableHai bisogno del valore più popolare in mysql valore separato da virgola?

| id | tags   
| --- | ------------ 
| 1 | css,html,php 
| 2 | php,java,css  
| 3 | java,c++,ios 

needout messo come

| tags | tags   
| --- | ------------ 
| css | 2 
| php | 2  
| java | 1 
| html | 1 
| c++ | 1 
| ios | 1 
+0

http://php.net/manual/en/function.array-count-values.php – rahul

+0

normalizzare il database per evitare di memorizzare valori separati da virgola e prevenire complessità . –

+1

Se stavo andando * lì *, non vorrei iniziare da * qui * – Strawberry

risposta

2

Non sono sicuro che l'estensione DB che si sta utilizzando. Potete provare questo -

// Fetch the data by executing- 
"SELEC GROUP_CONCAT(tags) tags FROM my_tags"; 

// Then explode them 
$tags = $row['tags']; 
$tags_array= explode(',', $tags); 

//Count the values 
$counts = array_count_values($tags_array); 

// Sort 
$counts = arsort($counts); 

Questo è come un algoritmo. Implementalo con il tuo codice.

1

Prova questo .. Spero che ti sia d'aiuto.

SELECT  `value`, 
      COUNT(`value`) AS `value_occurrence` 
    FROM  `my_table` 
    GROUP BY `value` 
    ORDER BY `value_occurrence` DESC 
    LIMIT 1; 
+0

Questo non sarebbe di aiuto come 'tag' hanno valori diversi. Quindi 'COUNT' sarebbe' 1' per tutti i valori. –

+0

Per ottenere un elenco di valori e il numero delle loro apparizioni: Provalo. select col_name, count (col_name) c dalla tabella group by col_name ordina per c desc; –

+1

Si prega di verificare questo http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_find-in-set –

1

Ciò può aiutare: select group_concat(tags) from tags group by tag;

Problemi correlati