#1054-未知列' lng'在'字段列表'获取时出错

时间:2015-10-09 11:52:19

标签: mysql latitude-longitude

我试图使用此查询让用户在一定范围内

SELECT *, (3959 * acos(cos(radians(10.916089)) * cos(radians(lat)) * cos( radians(lng) - radians(57.598873)) + sin(radians(10.916089)) * 
sin(radians(lat)))) 
AS distance 
FROM project_details WHERE distance < 15 ORDER BY distance LIMIT 0 , 10

但我收到此错误

  

#1054-未知专栏&#39; lng&#39;在&#39;字段列表&#39;提取错误

我做错了什么?

我的lat和lng的DB数据类型分别是十进制(10,8)和十进制(11,8)。

    CREATE TABLE `project_details` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `description` varchar(250) NOT NULL,
  `startDate` datetime NOT NULL,
  `endDate` datetime NOT NULL,
  `address` varchar(100) NOT NULL,
  `zipcode` int(10) NOT NULL,
  `lat` varchar(100) NOT NULL,
  `lng` varchar(100) NOT NULL,
  `status` varchar(50) NOT NULL,
  `phone` varchar(15) NOT NULL,
  `email` varchar(250) NOT NULL,
  `createdBy` int(10) NOT NULL,
  `createdAt` datetime NOT NULL,
  `updatedAt` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `createdBy` (`createdBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我尝试重新创建您的案例,但lngWHERE conditionSELECT *, (3959 * acos(cos(radians(10.916089)) * cos(radians(lat)) * cos( radians(lng) - radians(57.598873)) + sin(radians(10.916089)) * sin(radians(lat)))) AS distance FROM project_details WHERE (3959 * acos(cos(radians(10.916089)) * cos(radians(lat)) * cos( radians(lng) - radians(57.598873)) + sin(radians(10.916089)) * sin(radians(lat)))) < 15 ORDER BY distance LIMIT 0 , 10; 无关。您不能在where condition中使用别名,您需要使用相同的表达式或使用子查询。

<强> SqlFiddleDemo

SELECT sub.* 
FROM 
(
  SELECT *, (3959 * acos(cos(radians(10.916089)) * cos(radians(lat)) * cos( radians(lng) - radians(57.598873)) + sin(radians(10.916089)) * 
sin(radians(lat)))) 
  AS distance 
  FROM project_details 
) AS sub
WHERE sub.distance < 15 
ORDER BY sub.distance 
LIMIT 0 , 10

使用子查询:

2000