是的,可以在查询中计算相关性。要了解查询中的相关性,您需要首先创建一个表。创建表的查询如下
mysql> create table correlationDemo - > ( - > value float not null, - > value2 float not null - > );
借助insert命令将一些记录插入表中。查询如下,在表中插入记录
mysql> insert into correlationDemo values(1,10); mysql> insert into correlationDemo values(2,4); mysql> insert into correlationDemo values(3,5); mysql> insert into correlationDemo values(6,17);
使用select语句显示表中的所有记录。
查询如下
mysql> select *from correlationDemo;
以下是输出
+-------+--------+ | value | value2 | +-------+--------+ | 1 | 10 | | 2 | 4 | | 3 | 5 | | 6 | 17 | +-------+--------+ 4 rows in set (0.03 sec)
现在这是查询中的简单关联
mysql> select @firstValue:=avg(value), - > @secondValue:=avg(value2), - > @division:=(stddev_samp(value) * stddev_samp(value2)) from correlationDemo;
以下是输出
+-------------------------+---------------------------+-------------------------------------------------------+ | @firstValue:=avg(value) | @secondValue:=avg(value2) | @division:=(stddev_samp(value) *stddev_samp(value2)) | +-------------------------+---------------------------+-------------------------------------------------------+ | 3 | 9 | 12.84090685617215 | +-------------------------+---------------------------+-------------------------------------------------------+ 1 row in set (0.00 sec)
这是上述相关查询的计算
mysql> select sum( ( value - @firstValue ) * (value2 - @secondValue) ) / ((count(value) -1) * @division) from - > correlationDemo;
以下是输出
+--------------------------------------------------------------------------------------------+ | sum( ( value - @firstValue ) * (value2 - @secondValue) ) / ((count(value) -1) * @division) | +--------------------------------------------------------------------------------------------+ | 0.7008850777290727 | +--------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)