title: MySQL IF statement on a calculated column
body: <p>How can I use a calculated column in an SQL statement that uses an 'IF'Statement in the 'Where' clause.</p>
  
  <p>My query is down below. I keep getting the error</p>
  
  <p><code>Unknown column 'e_owner_id_church' in 'where clause'</code></p>
  
  <pre><code>SELECT idu,fname,lname,id_file,friendly_name,file_name,uploaded_by,date_uploaded,
  file_last_modified,owner_id, 
  CASE WHEN owned_by='church' THEN church_name WHEN owned_by='user' THEN CONCAT(fname, ' ', lname) END AS e_owner_name, 
  CASE WHEN owned_by='church' THEN id_church END AS e_owner_id_church,
  CASE WHEN owned_by='user' THEN idu END AS e_owner_id_user 
  FROM files,file_affiliations,churches,users 
  WHERE owner_id=IF(e_owner_id_church != null, e_owner_id_church, e_owner_id_user) 
  GROUP BY file_id
  </code></pre>