这是我的餐桌

pair count prob
a|||a 2
a|||book 1
a|||is 2
book|||a 1
book|||book 1
book|||is 1


例如:if I want to calculate a|||a prob = (a|||a count)/(when left string 'a' appearance)=2/(2+1+2)=0.4
并将此概率更新为第三列prob
,所以a|||book =1/(2+1+2) ....book|||is =(book|||is count)/when left sting 'book' appearance) =1/(1+1+1)

在MySQL代码中有没有适当的实现方法?

最佳答案

请按照以下步骤操作:
1.创建表sum_of_counts

create table
sum_of_counts(wild_pair varchar(10),sum int);


2.运行此查询以计算计数总和并将其插入表sum_of_counts

insert into sum_of_counts
  select substring(pair,1,locate('|',pair)),sum(count)
   from em group by substring(pair,1,locate('|',pair))



用概率更新您的表em

更新em set prob = count /从sum_of_count中选择总和,其中wild_pair = substring(pair,1,locate('|',pair))

关于mysql - 如何使用MySQL计算复合字符串列的概率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36994567/

10-16 22:58