title: Unknown column where clause (MySQL)
body: <p>I have this MySQL query. But, I'm wondering why I get this error message.</p>
  
  <blockquote>
    <p>Unknown column 'min_pp_distance' in 'where clause'</p>
  </blockquote>
  
  <p>I'm pretty sure I have this variable in the query.</p>
  
  <pre><code> IF(PP.latitude &lt;&gt; 0 AND PP.longitude &lt;&gt; 0, ( 6371 * acos( cos( radians(3.5951956) ) * cos( radians( PP.latitude ) ) * cos( radians( PP.longitude ) - radians(98.6722227) ) + sin( radians(3.5951956) ) * sin( radians( PP.latitude ) ) ) ) , 9999999) as min_pp_distance
  </code></pre>
  
  <p><strong>Here is the complete query (I tidy up and remove some unnecessary variables)</strong></p>
  
  <pre><code>SELECT SQL_CALC_FOUND_ROWS D.id * -1 as id, D.doc_title as doc_title, 
  NULL as user_email, D.first_name, D.last_name,
  
  ...
  CONCAT_WS('#', PP.id, PP.name) as pp_name, CONCAT_WS('#', PP.id, PP.address) 
  as pp_address, CONCAT_WS('#', PP.id, PP.phone) as pp_phone, CONCAT_WS('#', 
  PP.id, V.name)as pp_vil, CONCAT_WS('#', PP.id, SD.name) as pp_sub_d, 
  CONCAT_WS('#', PP.id, C.name) as pp_city, CONCAT_WS('#', PP.id, P.name) as 
  pp_province, CONCAT_WS('#', PP.id, PP.zipcode) as pp_zip, 1 pp_is_primary, 
  CONCAT_WS('#', PP.id, PP.latitude) as pp_lat, CONCAT_WS('#', PP.id, 
  PP.longitude) as pp_lng, CONCAT_WS('#', PP.id, PP.type) as pp_type, 
  CONCAT_WS('#', PP.id,
  
  IF(PP.latitude &lt;&gt; 0 AND PP.longitude &lt;&gt; 0, ( 6371 * 
  acos( cos( radians(3.5951956) ) * cos( radians( PP.latitude ) ) * cos( 
  radians( PP.longitude ) - radians(98.6722227) ) + sin( radians(3.5951956) ) 
  * sin( radians( PP.latitude ) ) ) ), 0)) as pp_distance, IF(PP.latitude &lt;&gt; 0 AND PP.longitude &lt;&gt; 0, ( 6371 * acos( cos( radians(3.5951956) ) * cos(radians( PP.latitude ) ) * cos( radians( PP.longitude ) - 
  radians(98.6722227) ) + sin( radians(3.5951956) ) * sin( radians( 
  PP.latitude ) ) ) ) , 9999999) as min_pp_distance,
  
  ....
   D.status as reg_status FROM register_doctor D LEFT JOIN 
   ref_doctor_practice_place RDPP ON RDPP.doctor_id = D.id * -1 LEFT JOIN 
   practice_place PP ON PP.id = RDPP.practice_place_id LEFT JOIN village V ON 
   V.id = PP.village_id LEFT JOIN sub_district SD ON SD.id =PP.sub_district_id 
   LEFT JOIN city C ON C.id = SD.city_id INNER JOIN province P ON P.id = 
   PP.province_id INNER JOIN city RDC ON RDC.id = D.city_id AND RDC.name LIKE 
   '%Medan%' INNER JOIN province RDP ON RDP.id = RDC.province_id AND ( RDP.name 
  LIKE 'Sumatera Utara%' OR RDP.long_name LIKE 'Sumatera Utara%' ) INNER JOIN 
  ...
  WHERE D.status = 2 AND min_pp_distance &lt; 50 OR min_pp_distance = 9999999 GROUP BY D.id ORDER BY min_pp_distance ASC, RAND() LIMIT 0,10
  </code></pre>
  
  <p><strong>Here is the CREATE TABLE:</strong></p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `register_doctor` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `doc_title` int(11) NOT NULL,
    `first_name` varchar(35) NOT NULL,
    `last_name` varchar(35) DEFAULT NULL,
    `gender` int(11) NOT NULL,
    `city_id` int(11) NOT NULL,
    `province_id` int(11) NOT NULL,
    `status` int(11) NOT NULL COMMENT '0 = Pending; 1 = Verified, 2 = Not Reg Yet, 3 = Pending Approval',
    `str_number` char(6) DEFAULT NULL,
    `editted_by` int(11) DEFAULT NULL,
    `editted_date` bigint(20) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `city_id` (`city_id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=179327 ;
  
  CREATE TABLE IF NOT EXISTS `practice_place` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(75) NOT NULL,
    `statement` text,
    `address` varchar(200) NOT NULL,
    `phone` varchar(15) NOT NULL,
    `fax` varchar(15) NOT NULL,
    `email` varchar(50) NOT NULL,
    `village_id` varchar(50) NOT NULL,
    `sub_district_id` varchar(50) NOT NULL,
    `province_id` varchar(50) NOT NULL,
    `zipcode` varchar(10) NOT NULL,
    `website` varchar(50) NOT NULL,
    `latitude` double NOT NULL,
    `longitude` double NOT NULL,
    `type` int(11) NOT NULL,
    `managed_by` int(11) DEFAULT '0',
    `doctor_group_id` int(11) NOT NULL,
    `category` varchar(50) NOT NULL,
    `photo_file` char(36) NOT NULL,
    `is_branch` int(11) NOT NULL,
    `parent_id` int(11) NOT NULL,
    `editted_by` int(11) NOT NULL,
    `editted_date` bigint(20) NOT NULL,
    `status` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `village_id` (`village_id`),
    KEY `doctor_group_id` (`doctor_group_id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=25557 ;
  
  CREATE TABLE IF NOT EXISTS `ref_doctor_practice_place` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `doctor_id` int(11) NOT NULL,
    `practice_place_id` int(11) NOT NULL,
    `is_primary` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `doctor_id_2` (`doctor_id`,`practice_place_id`),
    KEY `doctor_id` (`doctor_id`),
    KEY `practice_place_id` (`practice_place_id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=38840 ;
  </code></pre>
  
  <p>Does anyone see what I don't see?</p>