zoukankan      html  css  js  c++  java
  • 今天面试的一道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;
  • 相关阅读:
    FreeNAS的安装使用记录
    slackware中配置命令行下的分辨率
    创建类的理由
    软件开发人员的入门级书单
    Pow(x, n)
    Group Anagrams
    Rotate Image
    Permutations
    Multiply Strings
    Combination Sum II
  • 原文地址:https://www.cnblogs.com/ymj0906/p/3017265.html
Copyright © 2011-2022 走看看