title: if value is not empty, then insert mysql
body: <p>I have a table in mysql like this (the id is primary key):</p>
  
  <pre><code> id | name | age
   1 |  John | 46
   2 |       | 56
   3 | Jane  | 25 
  </code></pre>
  
  <p>Now I want to update the name only if this is empty. If the value is not empty it should duplicate the row with a new id else it should update the name.</p>
  
  <p>I thought it could be done with an if-statement but it doesn't work.</p>
  
  <pre><code>if((select `name` from `table1` where `id` = 3) = '',
  update `table1` set `name`='ally' where `id` = 3, 
  insert into `table1` (`id`,`name`,`age`) values 
  (4, 'ally', select `age` from `table1` where `id` = 3))
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>With Spencers answer I made it working using an if in the code. (However I would still like a way to do just a single mysql query).</p>
  
  <pre><code>db.set_database('database1')
  cursor = db.cursor()
  
  query = "select IF(CHAR_LENGTH(name)&gt;0,1,0) from table1 where id = {0}".format(id)
  cursor.execute(query)
  val1 = cursor.fetchone()
  
  if val1[0]:
      query = "INSERT INTO `table1` (`id`,`name`,`age`) SELECT {0},{1},`age` FROM `table1` WHERE `id` = {2}".format(new_id, name, id)
      cursor.execute(query)
  else:
      query = "update `table1` set `name` = '{0}' where `id` = {1}".format(name, id)
      cursor.execute(query)
  
  db.commit()
  </code></pre>