title: The following SQL statement used to work, but now I am getting an unknown column error for a column that I know exists
body: <p>I did not write this code initially, but it did work at one time.  Unfortunately it is no longer working.  My research suggests it is a mismatch in SQL types, but I am not well versed enough in SQL to fix the error.  Hopefully someone here can direct me.</p>
  
  <pre><code>SELECT DISTINCT (`users`.`card_id` ) AS `card_id2`
  , `users` .`firstname` AS `First Name`
  , `users`.`lastname` AS `Last Name`
  , `users`.`card_id` AS `Card ID`
  , `users`.`address1` AS `Address`
  , `users`.`address2` AS `Address2`
  , `users`.`city` AS `City`
  , `users`.`state` AS `State`
  , `users`.`zip` AS `Zip`
  , `users`.`email` AS `Email`
  , `users`.`phone_area`
  , `users`.`phone_exchange`
  , `users`.`phone_number`
  , `trans`.`trans_dt` AS `Last Trans Date`
  , `trans`.`credit_balance` AS `Credit Balance`
  , `trans`.`balance` AS `Gift Balance`
  , `users`.`gender` AS `Gender`
  , `users`.`birthdate` AS `Birthday (Age)`
  , `users`.`register_dt` AS `Registration Date`
  , `merchants`.`company`
  , `merchants`.`merchantkey`FROM `trans`
  , `users`
  , (SELECT `card_id`, max( `trans_id` ) AS `trans_dt` 
     FROM `trans` 
     WHERE `trans`.`trans_type` &gt;0 GROUP BY `card_id`
  ) AS `trans_max` 
  LEFT JOIN `merchants` ON `merchants`.`merchantkey`=`trans`.`merchantkey` 
  WHERE trans.card_id = users.card_id 
  AND trans.card_id = trans_max.card_id 
  AND trans.trans_id = trans_max.trans_dt 
  ORDER BY users.lastname, users.firstname;
  
  
  
  Error Code: 1054. Unknown column 'trans.merchantkey' in 'on clause'
  </code></pre>