Be sure to use "order by id"


  • 3
    B

    There is a hidden trap: there is no default record ordering in mysql. So if we say "numbers that appear at least three times consecutively.", we better to add "order by id" here.

    http://stackoverflow.com/questions/8746519/sql-what-is-the-default-order-by-of-queries

    My solution:

    select 0 Num from dual where (@pre := null) is null and (@count := 1) is null
        union all
    select distinct t.Num from (select * from Logs order by Id) t
    where (@count := if(@pre = t.Num, @count + 1, 1)) > -1 and (@pre := t.Num) is not null and @count = 3
        union all
    select 0 from dual where (@pre := null) is null and (@count := null) is not null;
    

  • 0
    T

    I think you need the order by because of your generic solution. This is the easiest medium problem, so I would think 3 joins is an acceptable solution here, which orders correctly. Can you please explain how yours work or point to the documentation.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.