Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未知列where子句(MySQL)_Mysql - Fatal编程技术网

未知列where子句(MySQL)

未知列where子句(MySQL),mysql,Mysql,我有一个MySQL查询。但是,我想知道为什么我会收到这个错误消息 “where子句”中的未知列“min_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(

我有一个MySQL查询。但是,我想知道为什么我会收到这个错误消息

“where子句”中的未知列“min_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

有人看到我没有看到的吗?

谢谢。因此,您应该使用HAVING,或者考虑一种不同的方法来过滤查询。

您可以根据以下要点使用别名应用您的条件-

第1点:左连接后的内部连接没有逻辑,如果由于任何特定原因要在左连接后使用内部连接,请在左连接前使用内部连接进行子查询

第2点:你应该正确地指定你的OR条件,或者它只是第二个条件,或者两者都是and条件,试着用括号来区分它

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

我看到一大块代码,没有CREATETABLE语句。很抱歉,回答这样的问题非常困难。您至少可以格式化SQL查询,使其可读。别指望我们为你做所有的工作,我只是做了大卫。对不起,谢谢。这就是我的答案。
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 ;
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