同一查询的where子句中的别名值

时间:2017-05-19 09:42:05

标签: mysql codeigniter

我想在同一查询中获取距离> = distance_filter的结果

SELECT SQL_CALC_FOUND_ROWS null as row, distance_filter, j.lat, j.long, ROUND((((acos(sin((28.53551600*pi()/180)) * sin((j.lat*pi()/180)) + cos((28.53551600*pi()/180)) * cos((j.lat*pi()/180)) * cos(((77.39102600 - j.long) * pi()/180))))*180/pi())*60*1.1515*1.609344 )) as distance, j.job_id, j.brand, j.location, j.model, j.creation_time
FROM `mb_job` as `j`
JOIN `mb_job_issue` as `ji` ON `j`.`job_id`=`ji`.`job_id`
WHERE `j`.`creation_time` >= '2017-05-18 09:39:43'
AND `j`.`status` = '1'
AND `j`.`job_id` NOT IN('')
GROUP BY `j`.`job_id`
HAVING `distance` <= 'DISTANCE_FILTER'
ORDER BY `j`.`job_id` DESC
 LIMIT 10

提前感谢任何指导

2 个答案:

答案 0 :(得分:2)

您只需将having子句放在单引号内。这个查询在我的最后工作正常

SELECT SQL_CALC_FOUND_ROWS null as row, distance_filter, j.lat, j.long, ROUND((((acos(sin((28.53551600*pi()/180)) * sin((j.lat*pi()/180)) + cos((28.53551600*pi()/180)) * cos((j.lat*pi()/180)) * cos(((77.39102600 - j.long) * pi()/180))))*180/pi())*60*1.1515*1.609344 )) as distance, j.job_id, j.brand, j.location, j.model, j.creation_time
FROM `mb_job` as `j`
JOIN `mb_job_issue` as `ji` ON `j`.`job_id`=`ji`.`job_id`
WHERE `j`.`creation_time` >= '2017-05-18 09:39:43'
AND `j`.`status` = '1'
AND `j`.`job_id` NOT IN('')
GROUP BY `j`.`job_id`
HAVING 'distance <= distance_filter'
ORDER BY `j`.`job_id` DESC
 LIMIT 10

希望它可以帮到你

答案 1 :(得分:1)

您必须使用像

这样的整体计算

WHERE ROUND((((acos(sin((28.53551600*pi()/180)) * sin((j.lat*pi()/180)) + cos((28.53551600*pi()/180)) * cos((j.lat*pi()/180)) * cos(((77.39102600 - j.long) * pi()/180))))*180/pi())*60*1.1515*1.609344 )) >= distance filter

或者您可以将其转移到HAVING子句: HAVING distance >= distance_filter