I am trying to put together a query that does the following:
I have a table with 512 entries in it (initially, this will increase with records at we add to it)
This table has columns id, ip_address, in_use where (id is auto_incrementing and primary) and in_use is bool true / false.
Basically I have a list of addresses in this (like I said initially it's got 512)
I need to ask mysql to return rows not in use, but group them together by a set number (ie I need 11 free addresses),
However, this system will have rows marked false on the in_use col right next to others with true.
I need a way to grab a set amount of IP's the id being broken sequentially... example
ids => in_use
1 => false
2 => false
3 => false
4 => false
5 => false
6 => false
7 => false
8 => false
9 => true
10 => true
11 => false
12 => false
13 => false
14 => false
15 => false
16 => false
17 => false
18 => false
19 => false
20 => false
21 => false
22 => false
23 => false
Now if I query for 11 free addresses, closest to the top, it will bring back the list of id's 1 to 12 skipping over 9, this won't work for what I am trying to do :(
What I would like it to do is realize that 9 is in use so cannot use that, thus cannot make a sequential group and pick from 11 to 22... as that is the next available sequential set.
Is there any way to structure a query for this? Is it even possible in MySQL?
Without a fully flushed out solution I think I can say it may be possible and get you started.
If you add a rowcount column to the query output and then take the last part of the ip and subtract the rownumber, that should give you a constant number for contiguous ip addresses. Group by that column and add a count to see contiguous blocks. Change the ip to max(ip), ad a min(ip) and you should get start and end and counts of all the blocks. I won't be back at a PC until tomorrow to test all this but I think I will work and someone may be able to write it up before then ifyou can't figure it out.
SELECT @rownum:=@rownum+1 rownum, max(ip_address),min(ip_address), (substring_index(ip_address,-1)-rownum) as constant
FROM (SELECT @rownum:=0) r, tablename
Where in_use = 0
Group by constant
Change the tablename. I this works without tweakin I'll be amazed as I'm typing this only phone.
0
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
Can you try this query? Replace table123 with your actual table.
SELECT t4.* FROM table123 t4, (SELECT MIN(ids) ids FROM (SELECT ids, IFNULL((SELECT 'no' FROM table123 t2 WHERE t2.ids BETWEEN t1.ids AND t1.ids + 10 AND in_use = 'true' LIMIT 1),'yes') newcol FROM table123 t1) t3 WHERE newcol = 'yes') t5 WHERE t4.ids BETWEEN t5.ids AND t5.ids + 10;
2. Then, when marking a record with ID x not in use:
# Get first used IP address after current record with ID x.next_used_id : SELECT MIN(id) FROM ip_addresses WHERE in_use IS TRUE AND id > x;# Get contiguous_unused for current record.current_contiguous_unused : SELECT COUNT(*) + 1 FROM ip_addresses WHERE in_use IS FALSE and id > x and id < next_used_id;# Set contiguous_unused of current record.UPDATE ip_addresses SET contiguous_unused = current_contiguous_unused WHERE id = x;# Get last used IP address before current record with ID x.last_used_id : SELECT MAX(id) FROM ip_addresses WHERE in_use IS TRUE AND id < x;# Increment contiguous_unused by current_contiguous_unused for all records after last_used_id until previous record.UPDATE ip_addresses SET contiguous_unused = contiguous_unused + current_contiguous_unused WHERE id > last_used_id AND id < x;
Essentially, you would be adjusting the cached count for records with affected counts.
3. When marking a record with ID x as in use:
# Get last used IP address before current record with ID x.last_used_id : SELECT MAX(id) FROM ip_addresses WHERE in_use IS TRUE AND id < x;# Set contiguous_unused of current record.UPDATE ip_addresses SET contiguous_unused = 0 WHERE id = x;# Decrement contiguous_unused by 1 for all records after last_used_id until previous record.UPDATE ip_addresses SET contiguous_unused = contiguous_unused - 1 WHERE id > last_used_id AND id < x;
This is a rough algorithm. Of course you will have to catch cases when last used or next used record is not existent (start or end of table) and optimize for mass inserts/updates.
You can have flexible queries with this too, if you want to get the smallest unused range or even middle depending on your method for space optimization.
Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.
If you add a rowcount column to the query output and then take the last part of the ip and subtract the rownumber, that should give you a constant number for contiguous ip addresses. Group by that column and add a count to see contiguous blocks. Change the ip to max(ip), ad a min(ip) and you should get start and end and counts of all the blocks. I won't be back at a PC until tomorrow to test all this but I think I will work and someone may be able to write it up before then ifyou can't figure it out.