mysql - Group by and Having clause in the same query -
schema:
place(pid, name, type, lat, lng, deleted)
i want select count of places, grouping them type , having distance of < 10 km particular lat, lng
query:
select count(p.type) count (place p) p.deleted != 1 , p.pid in ( select p2.pid, ifnull(acos(sin((18.5236 *pi()/180)) * sin((p2.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p2.lat *pi()/180)) * cos(((73.8478 - p2.lng)*pi()/180))) * 6371.009, 0) distance place p2 having `distance` < 10 ) group p.type;
error:
operand should contain 1 column(s)
that because selecting 2 columns i.e pid , distance in sub select query. without using 2nd select column how can calculate distance.
rewrite script this
select count(p.type) count, -- remove if not necessary sum(ifnull(acos(sin((18.5236 *pi()/180)) * sin((p.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p.lat *pi()/180)) * cos(((73.8478 - p.lng)*pi()/180))) * 6371.009, 0)) distance place p p.deleted != 1 group p.type having sum(ifnull(acos(sin((18.5236 *pi()/180)) * sin((p.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p.lat *pi()/180)) * cos(((73.8478 - p.lng)*pi()/180))) * 6371.009, 0)) < 10
Comments
Post a Comment