title: Converting an IP to a Long in MySQL
body: <p>I'm trying to use the <code>ip2country</code> table to show the country flags of the users on my website.</p>
  
  <p>The simplest thing I came up with is to write an SQL statement that takes the users from the session table and queries to see if their respective IP is in a certain range to figure out their country/flag.</p>
  
  <p>It's simple but also dangerous, because when there are 300 users online to show and I fetch them from the session table, querying their countries to display the flags, there will surely be a big memory usage.</p>
  
  <p>Now I tried this to do it in one single query: </p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT 
    s.session_ip, 
    ipc.*
  FROM 
    session AS s
      LEFT JOIN ip2country AS ipc 
      ON ipc.ip_lo &lt;= s.session_ip AND ipc.ip_hi &gt;= s.session_ip
  WHERE 
    s.session_time  &gt; '".( time() - 60) )."' 
  </code></pre>
  
  <p>Now it's clear that the above query is wrong because the IPs saved in the <code>ip2country</code> table is an integer, e.g. 1000013824, and the IPs stored in the session table are the string representations of the IPs, e.g. 193.169.0.0</p>
  
  <p>I know how to convert from an IP to a long in PHP with the <code>ip2long()</code>, but is there any equivalent method in MySQL so I don't have to do two queries?</p>