Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts

    get existing / not existing ranges ?

    data
    Code:
    1
    2
    3
    8
    10
    11
    12
    expected result:
    Code:
    1-3
    8
    10-12
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    No good way to do this with a sql query. You could use a stored procedure, a cursor, and a temp table. But if you are going to do that you might as well just write the code in PHP to do the same thing. Simpler and likely more efficient.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Assuming 'table' is table with indexed int field id, this should work (3 sec. for 600.000 records)
    (not sure if that would work correct if id would be inserted into table in random order, not 1,2,3,.... like in my case)
    I presume this is not cross database compatible:

    Code:
    select *, CONCAT(counter,' - ', id-1)   from
    (
    select @num := @num + @num1 counter,
           i.id,
         
           if (@num <> i.id,@a :='a'          ,@a := 'b'         ) dd ,
    		 if (@num <> i.id,@num1 := (i.id-@num+1),@num1 := 1 ) c
         
    	    from table i
    , (SELECT @num := 0) d,(SELECT @num1 := 1) e
    order by id
    
    ) as z 
    where dd = 'a'
    produces thing like this (last column is wanted result - missing numbers, outer boundarys of missing intervals included):

    "2" "4" "a" "3" "2 - 3"
    "10" "13" "a" "4" "10 - 12"
    "89" "91" "a" "3" "89 - 90"
    "127" "128" "a" "2" "127 - 127"
    "322" "323" "a" "2" "322 - 322"
    "360" "361" "a" "2" "360 - 360"
    "363" "364" "a" "2" "363 - 363"
    "367" "368" "a" "2" "367 - 367"
    "377" "378" "a" "2" "377 - 377"
    "390" "391" "a" "2" "390 - 390"
    "410" "411" "a" "2" "410 - 410"
    "432" "433" "a" "2" "432 - 432"
    "471" "472" "a" "2" "471 - 471"
    "602" "603" "a" "2" "602 - 602"
    "604" "605" "a" "2" "604 - 604"
    "727" "729" "a" "3" "727 - 728"
    "803" "804" "a" "2" "803 - 803"
    "847" "848" "a" "2" "847 - 847"
    "866" "867" "a" "2" "866 - 866"
    "950" "951" "a" "2" "950 - 950"
    "975" "976" "a" "2" "975 - 975"
    ...
    Last edited by BubikolRamios; 10-24-2013 at 12:45 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •