MySQL更新基于其他列值的增量列

MCIT趋势

我有一个MySQL表,包括以下各列:

+------------+-------------+
|  auto_no   | auto_no_new |
+------------+-------------+
| 2021-10431 |       20577 |
| 2021-10432 |       20578 |
| 2021-10433 |       20579 |
| 2021-10434 |       20580 |
| 2021-10435 |       20581 |
| 2021-10436 |       20582 |
+------------+-------------+

“ auto_no”列中的值随相关年份而增加。表格中显示的值先前已开始,并且如上所述在年初开始更改。然后,我需要按以下方式启动“ auto_no”列中的值:

+------------+-------------+
|  auto_no   | auto_no_new |
+------------+-------------+
| 2021-00001 |       20577 |
| 2021-00002 |       20578 |
| 2021-00003 |       20579 |
| 2021-00004 |       20580 |
| 2021-00005 |       20581 |
| 2021-00006 |       20582 |
+------------+-------------+ 

我使用以下查询

update table set auto_no LIKE %'Y'- '????1'% where auto_no_new > 20577

但是没有得到想要的输出。可能出什么问题了?有人可以帮忙吗?

鲑鱼

似乎微不足道

drop table if exists t;
create table t(auto_no varchar(12), auto_no_new int);
insert into t values
( '2021-10431' ,       20577 ),
( '2021-10432' ,       20578 ),
( '2021-10433' ,       20579 ),
( '2021-10434' ,       20580 ),
( '2021-10435' ,       20581 ),
( '2021-10436' ,       20582 );

update t
        set auto_no = concat(substring_index(auto_no,'-',1),'-',lpad(auto_no_new - 20576,5,'0'))
where substring_index(auto_no,'-',1) = 2021;

select * from t;

+------------+-------------+
| auto_no    | auto_no_new |
+------------+-------------+
| 2021-00001 |       20577 |
| 2021-00002 |       20578 |
| 2021-00003 |       20579 |
| 2021-00004 |       20580 |
| 2021-00005 |       20581 |
| 2021-00006 |       20582 |
+------------+-------------+
6 rows in set (0.013 sec)

或者如果您不知道分钟数(auto_no_new)

update t cross join(select min(auto_no_new) - 1 minno from t where substring_index(auto_no,'-',1)  = 2021) s 
        set auto_no = concat(substring_index(auto_no,'-',1),'-',lpad(auto_no_new - s.minno,5,'0'))
where substring_index(auto_no,'-',1) = 2021;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

基于其他列值的估算值

来自分类Dev

基于其他列值的汇总

来自分类Dev

基于其他列的值的条件更改

来自分类Dev

基于其他列的 TSQL 聚合值

来自分类Dev

列值增量更新

来自分类Dev

想要基于网格集会上的新编辑值来更新其他列字段值

来自分类Dev

如何基于其他列的值更新列

来自分类Dev

mysql中基于相同ID的增量列值

来自分类Dev

MySQL SELECT基于不同列中的值的增量索引

来自分类Dev

MySQL-内部联接-使用基于其他值的值添加列

来自分类Dev

基于其他列将列中的值相乘

来自分类Dev

基于其他列将列中的值相乘

来自分类Dev

基于R中其他列的列值总和

来自分类Dev

Excel:基于其他列的列中的条件格式值

来自分类Dev

如何从其他两列更新基于文件名的文件名的最高值

来自分类Dev

基于其他列的MYSQL唯一列

来自分类Dev

MySQL - 基于其他列的订单自动填充列

来自分类Dev

熊猫:如何创建基于其他列值的条件对其他列求和的列?

来自分类Dev

基于DATE的MySQL更新值列

来自分类Dev

基于列中的某些值,更新另一个数据帧熊猫中的其他列

来自分类Dev

基于其他列条件和其他列计算的数据框新列值

来自分类Dev

熊猫多指标基于其他列中的值减去

来自分类Dev

添加列-指定名称-基于其他的行值

来自分类Dev

如何基于列值创建其他行或数据框

来自分类Dev

基于其他列值的熊猫条件填充

来自分类Dev

大熊猫基于其他列分配值

来自分类Dev

用基于其他列的值填充np.nan

来自分类Dev

查询以基于其他列获取最接近的匹配值

来自分类Dev

基于其他列值的列上的条件格式