-4

I have this MySQL query. But, I'm wondering why I get this error message.

Unknown column 'min_pp_distance' in 'where clause'

I'm pretty sure I have this variable in the query.

 IF(PP.latitude <> 0 AND PP.longitude <> 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

Here is the complete query (I tidy up and remove some unnecessary variables)

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 <> 0 AND PP.longitude <> 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 <> 0 AND PP.longitude <> 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 < 50 OR min_pp_distance = 9999999 GROUP BY D.id ORDER BY min_pp_distance ASC, RAND() LIMIT 0,10

Here is the CREATE TABLE:

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 ;

Does anyone see what I don't see?

yodann
  • 355
  • 5
  • 20

2 Answers2

2

You cannot use column alias in the WHERE clause. Therefore you should either use HAVING for that, or think of a different way to filter your query.

Community
  • 1
  • 1
uri2x
  • 3,162
  • 1
  • 11
  • 25
0

You can apply your conditions with aliases as per below but after considering below important points-

Point1 : There is no logic of inner join after left join, either use inner join before left join of make a sub-query if you want to use inner join after left join due to any specific reason.

Point 2: You should specify your OR condition properly either it is just for 2nd condition or with both and conditions, try to distinguish it by bracket.

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, S.name AS spc_name, S.short_desc AS spc_short_desc, 
GROUP_CONCAT(DISTINCT(S2.designation) SEPARATOR ', ') AS spc_dsg, 
GROUP_CONCAT(DISTINCT(EM.designation) SEPARATOR ', ') AS em_dsg, 
GROUP_CONCAT(DISTINCT(EF.designation) SEPARATOR ', ') AS ef_dsg, 
D.gender, 0 AS dob, 0 AS rating, 0 AS no_of_rating, NULL AS photo_file, 
NULL AS verification_status, 0 AS rating_score, PP.id AS pp_id, 
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 <> 0 AND PP.longitude <> 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 <> 0 AND PP.longitude <> 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, 
NULL AS dp_week, NULL AS 'comment', 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 ref_doctor_specialty RDS ON RDS.doctor_id = (D.id * -1) AND RDS.is_primary = 1 
INNER JOIN specialty S ON S.id = RDS.specialty_id 
INNER JOIN ref_doctor_specialty RDS2 ON RDS2.doctor_id = (D.id * -1) 
INNER JOIN specialty S2 ON S2.id = RDS2.specialty_id 
LEFT JOIN ref_doctor_education RDE ON RDE.doctor_id = (D.id * -1) 
LEFT JOIN edu_magister EM ON RDE.type = 4 AND EM.id = RDE.ref_edu_id 
LEFT JOIN edu_fellowship EF ON RDE.type = 5 AND EF.id = RDE.ref_edu_id 
GROUP BY D.id 
HAVING D.status = 2 AND min_pp_distance < 50 OR min_pp_distance = 9999999 
ORDER BY min_pp_distance ASC, RAND() 
LIMIT 0,10
Zafar Malik
  • 6,734
  • 2
  • 19
  • 30