需要在MySQL的逗号分隔值中找到最常见的值吗?

5

我的标签表

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

需要输出类似于以下的结果:
| tags  | tags         
| ---   | ------------ 
| css   |  2  
| php   |  2    
| java  |  1
| html  |  1
| c++   |  1
| ios   |  1

http://php.net/manual/en/function.array-count-values.php - rahul
规范化数据库以避免存储逗号分隔的值并防止复杂性。 - Abhik Chakraborty
1
如果我要去那里,我不会从这里开始。 - Strawberry
3个回答

2

不确定您正在使用哪个数据库扩展。您可以尝试以下方法 -

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

这就像一个算法。用你的代码实现它。

1
这可能会有所帮助: 按标签分组选择group_concat(tags) from tags;

1
尝试这个...希望它能有所帮助。
SELECT       `value`,
             COUNT(`value`) AS `value_occurrence` 
    FROM     `my_table`
    GROUP BY `value`
    ORDER BY `value_occurrence` DESC
    LIMIT    1;

这并没有帮助,因为“tags”具有不同的值。因此,“COUNT”对于所有值都将是“1”。 - Sougata Bose
获取值列表及其出现次数:试一下。选择 col_name,count(col_name) c from table group by col_name order by c desc; - Rahul
1
请查看此链接:http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_find-in-set - Rahul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接