【mysql】二 select * from (select * from table) as table2 使用 与 convert、cast的String转INT使用

需求:

查询某列最大值+1,做为新纪录的列值(分布式环境,一条语句实现)

问题一:先查询到最大值+1

select nextCode from (select max(projcode)+1 as nextCode from sm_sys_project where syscode=‘000’ ) as t

要点:取别名 as tableName (略取as也可)
eg:
select max+1 from (select max(projcode) as max from sm_sys_project where syscode=‘000’ ) t
也能实现

问题二:projcode列是String ,select ‘9’>‘10’ 是1为真,会报主键重复

需要String 转 int
三种方式:
①String + int 转int
select ‘9’+0>‘10’+0
结果:0 假

②cast( String as INT)
select cast(‘9’ as UNSIGNED) > cast(‘10’ as UNSIGNED)
结果:0 假

③convert(String , INT)
select convert(‘9’,UNSIGNED) > convert(‘10’,UNSIGNED)
结果:0 假<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值