I am trying to get the zip codes that are within a certain latitude and longitude and then using the zip codes returned pull all posts within the given area. The numbers for latitude and longitude are variables but for testing purposes they are hard numbers.
Can someone guide me in the right direction to make this work?
Query #1 – works selects post
SELECT user_id, session, zip, price, city, state, post_id, category, shortDesc, fpi
FROM post ORDER BY post.post_id DESC;
Query #2 – works selects zip codes in area
SELECT count(value) as duplicate, value
from (SELECT DISTINCT zipcode as value from zip
WHERE latitude BETWEEN 27.747 AND 28.147
UNION all
SELECT DISTINCT zipcode FROM zip
WHERE longitude BETWEEN -82.657 AND -82.257)
as tbl group by value having count(value) > 1;
Query as subquery
Tried: Failed
SELECT user_id, session, zip, price, city, state, post_id, category, shortDesc, fpi
FROM post
WHERE zip = (
SELECT count(value) as duplicate ,value
from (SELECT DISTINCT zipcode as value from zip
WHERE latitude BETWEEN 27.747 AND 28.147
UNION all SELECT DISTINCT zipcode FROM zip
WHERE longitude BETWEEN -82.657 AND -82.257)
as tbl group by value having count(value) > 1)
ORDER BY post.post_id DESC;
error #1241 – Operand should contain 1 column(s)
Next Try: FAILED
SELECT user_id, session, zip, price, city, state, post_id, category, shortDesc, fpi
FROM post WHERE zip = (select distinct zipcode from zip
where latitude between 27.747 AND 28.147)
IN ( select distinct zipcode from zip
where longitude between -82.657 AND -82.257);
1242 – Sub query returns more than 1 row
Next Try: FAILED
SELECT user_id, session, zip, price, city, state, post_id, category, shortDesc, fpi
FROM post
WHERE zip = ANY (
select distinct zipcode from zip
where latitude between 27.747 AND 28.147)
IN ( select distinct zipcode from zip
where longitude between -82.657 AND -82.257);
1064 – You have an error in your SQL syntax;
I’m not sure what you want
try this