0

I have the task of comparing the IP address with the CIDR in the database (MYSQL) to get the value of the access technology. for example, I got a client's 10.1.2.5. And in the list of networks I have a list:

10.1.0.0/24 ETHERNET
10.1.1.0/24 WIFI
10.1.2.0/24 WIFI

I understand how to store this pair in the database. And then how can I compare the lines from the base with the value of the IP address. pls help me

  • Is https://en.wikipedia.org/wiki/Longest_prefix_match what you are looking for? So given a IP address (doesn't matter if host or subnet) it will return the subnet-IP address from your lookup table. – CodingTil Sep 09 '21 at 13:30
  • 2
    Does this help https://stackoverflow.com/questions/595748/is-there-way-to-match-ip-with-ipcidr-straight-from-select-query – RiggsFolly Sep 09 '21 at 13:31
  • @RiggsFolly It looks like this is what I was looking for. Several replies in one topic at once. I will study and test this theory, and give feedback. – Andrew Zyuzenkov Sep 09 '21 at 13:35

1 Answers1

0
SELECT name
FROM test
WHERE ip = INET_ATON(@address) & mask;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a75ea7c00b7447a98e1c0a0ec8d4336f

Akina
  • 39,301
  • 5
  • 14
  • 25