Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alias value in where clause for the same query

I want to get result where distance >=distance_filter which we are fetching in same query

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

Thanks in advance for any guidance

like image 661
Jailendra Rajawat Avatar asked Oct 18 '22 12:10

Jailendra Rajawat


1 Answers

You just need to put having clause inside a single quote. This query is working fine at my end

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

Hope it helps you

like image 163
Akhilesh Jha Avatar answered Oct 21 '22 05:10

Akhilesh Jha