title: Automatically set the value of a column from a function or query
body: <p>I have a MariaDB database and I have table <code>userInfo</code> which has a column of <code>ipAddress</code> and <code>country</code>. I want to have another column named <code>country</code> and automatically get the country from the <code>ipAddress</code> and insert it in the <code>country</code> column. I already imported GeoIPLite2 and already have a SQL Query but I dont know how to use make mariadb do it instead of doing it on my code.</p>
  <pre><code>SELECT a.country_name
  FROM geoipcountries a, geoipblocks b
  WHERE INET_ATON('1.2.3.4') BETWEEN network_start_integer AND network_last_integer
  AND b.geoname_id = a.geoname_id
  </code></pre>
  <p>This query give me the country name.
  I want the MySQL to set the value of <code>country</code> column in <code>userInfo</code> table from the output of the query above.</p>
  <p>I already wrote this function as well:</p>
  <pre><code>DELIMITER $$
  CREATE FUNCTION getIpCountry(ip varchar(15)) RETURNS varchar(64)
  BEGIN
  declare result varchar(64);
  select SQL_CACHE a.country_name into result FROM geoipcountries a, geoipblocks b WHERE INET_ATON(ip) BETWEEN network_start_integer AND network_last_integer AND b.geoname_id = a.geoname_id;
  if (result is null) or (result = '') then
  set result := 'unknown';
  end if; 
  return result;
  END$$
  DELIMITER ;
  </code></pre>