Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

[PHP] MySQL Ranges

SpaceshipSpaceship
NamecheapNamecheap
Namecheap AuctionsNamecheap Auctions
Watch

beaver6813

Established Member
Impact
16
Hi Guys,

I've got a database full of numbers that should be contiguous, but some aren't ie:
Code:
23
24
25
26
27
29
30
31
32

I need to get the ranges of this, ie.
Code:
24-27 29 30-32
I've had a look about but I can't find any implementations efficient enough for use on large amounts of data, any ideas what MySQL queries could achieve this?
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
It would be tough to achieve the intended result just with a query. You'll need coding. You would need to sort the numbers, then when there is a gap you handle it accordingly.
 
0
Close
0
Close
that's just genius, I love to see tricks like this
 
0
Close
That's a great solution.

Just curious though, would creating a temporary table for a very large query have a performance hit?
 
0
Close
That's a great solution.

Just curious though, would creating a temporary table for a very large query have a performance hit?

Just put that to the test... unfortunately yes, significantly even on only 6000 records it took about 5 seconds to process. So I'm trying to find another pure MySQL solution, any ideas? :D

EDIT: Just for future reference I found the best solution was in the end to retrieve all the numbers from the database and then loop over them all creating ranges. Its a lot faster than the queries, most probably because its all in memory after it retrieves the results from the database.
 
Last edited:
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Search: better matches first
Search now shows better matches first. Threads with your words in their title now come near the top. A poll with your words in its...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back