简体   繁体   English

MySQL,在一列上应用 function,然后为每一行更新另一列

[英]MySQL, apply a function on one column and then update another column for each row

Two of a table's columns are named csv and s.表的两个列分别命名为 csv 和 s。 csv contains comma separated 1 digit numbers as a string, no duplication, like '1,5,3,2', there is no duplication like '1,5,1'. csv 包含逗号分隔的 1 位数字作为字符串,没有重复,如 '1,5,3,2',没有重复,如 '1,5,1'。 Want to count how many 1s and 3s there are in 'csv' and put the count into column 's'.想计算“csv”中有多少个 1 和 3,并将计数放入“s”列。 It's like to apply a function to each 'csv' and put the result into 's' for each row.这就像将 function 应用于每个“csv”并将结果放入每行的“s”中。 I know how to count, but had never seen how to apply a function to one column and put result into another column.我知道如何计算,但从未见过如何将 function 应用于一列并将结果放入另一列。 This is done in stored procedure, do I have to go through it row by row.这是在存储过程中完成的,我是否必须逐行通过它 go。

Just:只是:

update mytable 
set s = char_length(csv) - char_length(replace(replace(csv, '1', ''), '3', ''))

The formula compares the number of characters in column csv to the number of characters after removing 1 s and 3 s.该公式将csv列中的字符数与去除1 s 和3 s 后的字符数进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM