Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need most popular value in mysql comma separated value?

Tags:

php

mysql

my tags table

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

need out put like

| tags  | tags         
| ---   | ------------ 
| css   |  2  
| php   |  2    
| java  |  1
| html  |  1
| c++   |  1
| ios   |  1
like image 981
Ravi Kadia Avatar asked Sep 26 '22 09:09

Ravi Kadia


2 Answers

Not sure what DB extension you are using. You can try this -

// 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);

This is like an algorithm. Implement it with your code.

like image 132
Sougata Bose Avatar answered Nov 03 '22 00:11

Sougata Bose


Try this.. Hope it will help.

SELECT       `value`,
             COUNT(`value`) AS `value_occurrence` 
    FROM     `my_table`
    GROUP BY `value`
    ORDER BY `value_occurrence` DESC
    LIMIT    1;
like image 29
Rahul Avatar answered Nov 03 '22 01:11

Rahul