Mysql 使用计算值更新列
以下是我使用的表格:Mysql 使用计算值更新列,mysql,sql,Mysql,Sql,以下是我使用的表格: +-------------+----------+---------------------+ | sourceindex | source | pa | +-------------+----------+---------------------+ | 0 | this | 0.13842974556609988 | | 1 | is | 0.26446279883384
+-------------+----------+---------------------+
| sourceindex | source | pa |
+-------------+----------+---------------------+
| 0 | this | 0.13842974556609988 |
| 1 | is | 0.26446279883384705 |
| 2 | a | 0.26446279883384705 |
| 3 | book | 0.13842974556609988 |
| 4 | , | 0.26446279883384705 |
| 5 | that | 0.13842974556609988 |
我想添加一列,它将作为结果log(sum(pa))/pa
有什么建议吗?您可以使用交叉连接来计算
log(sum(pa))
在外部,您可以将结果除以pa
列的每个值
update
test t
join (select
`sourceindex`, `source`, `pa` , log_sum/pa new_col
from
test
cross join (select log(sum(pa)) log_sum
from test ) a
) t1
on (t.sourceindex= t1.sourceindex
and t.source = t1.source
and t.pa = t1.pa
)
set t.new_col = t1.new_col
但是,如果您切换逻辑以使用select查询显示您的计算结果,效果会更好
select `sourceindex`, `source`, `pa` , log_sum/pa new_col
from
test
cross join (select log(sum(pa)) log_sum
from test ) t
添加列并定义进行计算的触发器。或者最好在select中进行计算,而不添加列
log(sum(pa))/pa
将为每行返回相同的值,是否希望为每行返回相同的值?