select 出现次数最多的词跨越多个列

select the words with the highest occurrence cross multiple columns

我有以下 table :

id  tag1     tag2    tag3
1   apple  orange   pears
2   orange  apple   pears
3   pears  orange   apple
4   orange  apple   pears
5   apple  orange   orange

我想要这个

tag      count
orange    6
apple     5
pears     4

我的查询无法正常工作

$res = mysql_query("SELECT tag, count(tag) occurrences
FROM
(
  SELECT col, tag
  FROM $tbl_name
  unpivot
  (
    tag
    for col in (tag1, tag2, tag3)
  ) unpiv
) d
GROUP BY tag 
order by occurrences desc");

基本上什么都没有输出....某处有错误。

MySQL 中没有 pivot 或 unpivot 函数。但是,您可以使用联合,类似于此处所做的:MySQL - turn table into different table

那么您可以按如下方式统计行数:

$res = mysql_query(select f.tag, count(f.tag) occurrences
from (
select id, tag1 as tag
from fruit
union
select id, tag2 as tag
from fruit
union
select id, tag3 as tag
from fruit
  ) f
group by f.tag
order by occurrences desc);

看看这个SQLfiddle看看:

http://sqlfiddle.com/#!2/feb03/20

与 Reislef 的建议类似,但在子查询中进行了一些求和。这应该(希望)意味着子查询中的元素可以使用索引进行计数,并且可以在不使用索引的情况下大大减少来自子查询的需要在子查询外求和的项目数。

SELECT f.tag, SUM(f.tag_count) AS occurrences
FROM 
(
    SELECT tag1 AS tag, COUNT(*) AS tag_count
    FROM fruit
    GROUP BY tag
    UNION ALL
    SELECT tag2 AS tag, COUNT(*) AS tag_count
    FROM fruit
    GROUP BY tag
    UNION ALL
    SELECT tag3 AS tag, COUNT(*) AS tag_count
    FROM fruit
    GROUP BY tag
) f
GROUP BY f.tag
ORDER BY occurrences DESC

请注意,使用 UNION ALL 很重要(UNION 会将 2 列或更多列中计数相同的任何项目视为要消除的重复项)