mysql - 基于多列计算行数
问题描述
我有一个包含相同标签集的多列表,如下所示:
id | tagone | tagtwo | tagthree |
----------------------------------------
0 | Action | Adventure| Rpg |
1 | Rpg | Action | Indie |
一行中永远不会有多个相同的标签。我想要的是返回每个标签的元素计数。我希望它独立于它所在的列并计算每一列。像这样:
Action |2 |
Adventure |1 |
Indie |1 |
Rpg |2 |
有没有办法在一个查询中做到这一点?
解决方案
这是一个糟糕的数据结构。您应该有一个表,每个 id 和每个标签都有一行。
同时,您可以取消此操作:
select tag, count(*)
from ((select id, tagone as tag from t) union all
(select id, tagtwo as tag from t) union all
(select id, tagthree as tag from t)
) tt
group by tag
推荐阅读
- python - 在测试执行期间重定向文件中的所有类型的异常和标准输出日志 - Python 3
- notepad++ - notepad++ 用户定义语言折叠打开和关闭
- i2c - 什么是广播地址,它在 I2C 中的用途是什么?
- python - 为什么 Python 只使用 4% 的 CPU?
- git - 更改推送到 GitHub 的提交...删除文件
- python - 在 Python 中从 .xlsx 文件中读取图表
- json - 使用 JSON 数组访问数据
- php - Imagemagick - 不知道如何解决
- wordpress - 403响应代码与http但不是在wordpress上的https
- python - 分组后如何获取所有行的数据框?