title: The optimization of the following queries
body: <p>I have database table IPv4:</p>
  
  <pre><code>id          int(11) PK
  ip          bigint(10)
  mask        bigint(10)
  broadcast   bigint(10)
  </code></pre>
  
  <p>I'm trying to make a text search of ip address, like user input 83.1.1.1/24 and I search for it using the following query:</p>
  
  <pre><code>SELECT `ip`, `broadcast` 
  FROM `IPv4` 
  WHERE 
      CONCAT(INET_NTOA(`ip`), "/", BIT_COUNT(`mask`)) LIKE '%:searchStr%'
  </code></pre>
  
  <p>Then I want to get the path to the found IP address (hierarchical tree):</p>
  
  <pre><code>SELECT DISTINCT `id` FROM `IPv4` WHERE (ip &lt;= :ip) AND (broadcast &gt; :broadcast)
  </code></pre>
  
  <p>Is it possible to make all of it in one query?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Moved forward:</p>
  
  <pre><code>SELECT DISTINCT t2.id
  FROM (
          SELECT ip, broadcast
          FROM IPv4
          WHERE CONCAT( INET_NTOA(  `ip` ) ,  "/", BIT_COUNT(  `mask` ) ) LIKE  '%:searchStr%'
      ) AS t1
  
  INNER JOIN IPv4 t2
  WHERE t2.ip &lt;= t1.ip AND t2.broadcast &gt;= t1.broadcast 
  ORDER BY t2.mask
  </code></pre>
  
  <p>This does the job, maybe possible even better?</p>