Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
矩阵的mysql查询_Mysql_Matrix - Fatal编程技术网

矩阵的mysql查询

矩阵的mysql查询,mysql,matrix,Mysql,Matrix,我有一些评估数据。对于每个问题1列,可能的值为(1,2,3,4,5)并存储在表格中 评估表: f1 f2 q3 ... 1 5 2 2 4 3 . . . 我想为2列生成1个矩阵: How often is f1=1 when q3=1? How often is f1=1 when q3=2? ... How often is f1=5 when q3=5? 所以在我的例子中是一个5*5的矩阵。 我如何用mysql正确地解决这个问题 我的第一次(有效)尝试是用25个工会对其施加暴力

我有一些评估数据。对于每个问题1列,可能的值为(1,2,3,4,5)并存储在表格中

评估表:

f1 f2 q3 ...
1  5  2
2  4  3
.
.
.
我想为2列生成1个矩阵:

How often is f1=1 when q3=1?
How often is f1=1 when q3=2?
...
How often is f1=5 when q3=5?
所以在我的例子中是一个5*5的矩阵。 我如何用mysql正确地解决这个问题

我的第一次(有效)尝试是用25个工会对其施加暴力,如:

SELECT count(0) FROM evaluation where q3 = 1 and f2 = 1
union all
SELECT count(0) FROM evaluation where q3 = 1 and f2 = 2
union all
...
union all
SELECT count(0) FROM evaluation where q3 = 5 and f2 = 5
但是,如何才能以一种好的方式做到这一点呢

SELECT f1, count(f1), f3
FROM evaluation
GROUP BY f3, f1

将为您提供f3所有值的f1计数。

这几乎是完美的查询,哇。现在唯一的一点是,我需要用零填充“不匹配”。我会设法找到解决办法。是否也可以不在列表中而在类似表的视图中获得结果?这种转换最好在客户端完成。sql更适合于获取数据。格式化/显示不是DB的用途。