您的位置:首页 > 职场人生

今天面试的一道mysql题目

2013-04-12 20:29 351 查看
  在一个叫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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: