SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem with my query...HELP?


Problem with my query...HELP?

Author
Message
aimkthrn
aimkthrn
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 5
Hi,
I have two tables:
1) ZIPCodes- ZIPCodes table contains all the zipcodes of the USA with latitude and longitude.
2) usersearch_answers- usersearch_answers table has a zipcode column with the zipcodes where users live.

I want to have two variables: zipcode that the user can specify, and a distance in miles that a user can also specify.
The results should return anyone withni the radus of the zipcode that the user specified. Now, the query below works as expected only when there's atleast one person in the users table with the zipcode that was specified. Nothing returns if the zipcode that entered is not contained in the table, even if there are nearby zipcodes. Can someone please pinpoint for me in this code where the problem is? What do I need to do in order to get the rest of the zipcodes working? Any help is greatly appreciated. Thanks in advance.
$data_query = mysqli_query($conn, "SELECT ZIPCodes.zipcode, usersearch_answers.username,
(3959 * acos(cos(radians(ZIPCodes.latitude)) *
cos(radians(center.latitude)) *
cos(radians(ZIPCodes.longitude ) -
radians(center.longitude)) +
sin(radians(ZIPCodes.latitude)) *
sin(radians(center.latitude)))) AS distance FROM
( ( SELECT usersearch_answers.username,
usersearch_answers.zipcode,
ZIPCodes.latitude,
ZIPCodes.longitude FROM
(usersearch_answers JOIN ZIPCodes ON usersearch_answers.zipcode = ZIPCodes.zipcode)
WHERE (ZIPCodes.zipcode='33133')
) center, ZIPCodes) INNER JOIN usersearch_answers ON ZIPCodes.zipcode = usersearch_answers.zipcode
WHERE (usersearch_answers.username <> '$userLoggedIn')
HAVING (distance < 5000)
ORDER BY distance");

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)

Group: General Forum Members
Points: 380908 Visits: 42229
I guess the first thing I would do is write a query that generates a list of zip codes based on the lat/long of the target zip code and distance. Then this can be used to identify the users based on zip codes.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
aimkthrn
aimkthrn
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 5
Thanks. I figured it out. How do you close this topic?

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)

Group: General Forum Members
Points: 380908 Visits: 42229

First, you post how you fixed your problem to help others that may come along and see this thread with a similar problem.
Second, you really don't "close" the thread



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search