java - 如何在值 90 后使子字符串 +1
问题描述
我正在尝试通过在最后一个数字之后添加 1 来生成数字,但代码不想在 90 之后添加 1 并保持生成 10
我的序列中的最后一个数字是 1918190 但我得到的新序列是 1918110
if (ecdCentre.getCenterID() == null || ecdCentre.getCenterID().isEmpty()) {
sql = "SELECT concat(EC_CENTRE_REGION_ID, EC_CENTRE_CONSTITUENCY_ID, lpad( CASE WHEN MAX(SUBSTRING(EC_CENTRE_NO,6)) = '' THEN 0 ELSE MAX(SUBSTRING(EC_CENTRE_NO,6)) END + 1 , 2, '0') ) centre_number FROM ecd_centre_reference WHERE EC_CENTRE_REGION_ID = ? AND EC_CENTRE_CONSTITUENCY_ID = ? ";
centreNumber = template.queryForObject(sql,
new Object[] { ecdCentre.getRegionID(), ecdCentre.getConstituencyID() }, String.class);
ecdCentre.setCenterID(centreNumber);
System.out.println("Getting the con code: " + ecdCentre.getConstituencyID());
System.out.println("Geting the re code: " + ecdCentre.getRegionID());
System.out.println("Centre Number: " + centreNumber);
}
解决方案
我通过转换表中的结果来解决它。首先,我必须将结果转换为 int,因为它以 varchar 格式存储
sql = "SELECT IFNULL(MAX(CONVERT( SUBSTRING(ec_centre_no, 6) , UNSIGNED INTEGER)), 0) + 1 new_centre_sequence FROM ecd_centre_reference "
+ " WHERE ec_centre_region_id = ? AND ec_centre_constituency_id = ? ";
```````````````````````````````````````````````````````````````
推荐阅读
- android - E/flutter(16289):[错误:flutter/lib/ui/ui_dart_state.cc(177)]未处理的异常:SocketException:套接字已关闭
- google-compute-engine - 为什么带有 COS 映像的 GCE VM 速度更快?
- swift - UICollectionViewDiffableDataSource cellProvider 比预期更频繁地调用
- edge-detection - Canny边缘检测器的计算复杂度
- git - git push 登录失败
- wpf - 如何在 F# 中使用 VisualTreeHelper 在 WPF 中查找父视觉对象?
- android - 如何在 bitbucket 管道中实现 android espresso 测试?
- ios - SwiftUI - 如果动画,视图会消失
- python - 当我想在 pycharm 中安装 cv2 时有什么问题?
- tensorflow - 将函数从 Python 转换为 TensorFlow