今天面试的一道mysql题目
在一个叫team的表有个int的字段(字段叫t_id)分别是1、2、4、5...200,最后一个数是200,一共100个数,请用mysql语句写出第一个不是顺序排列的数( 在这里就是4)?
我的想法是重新建个字段,模拟从1到100的连续数,然后和原字段比较,当然最后没有这样写,请问有什么更好的方法能用纯mysql语句解决呢?
解决有三种办法:
一:
set @num =0; select c from (select *,@num := @num+1 as c from team) as t where t_id <> c limit 1;
第一种方法可以写成一行:
select c from (select *,@i := @i+1 as c from team,(select (@i:=0)) t1) as t where t_id <> c limit 1;
二:
select t_id+1 as c from team having c not in(select t_id from team ) limit 1;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步