Selecting filtered results from a table with 1 million rows
Hello MySQL Gurus!
I am trying to write an interesting query here and I am not sure if it all can be done in one step in the same query. Here is my dilemma:
I have a DB with two tables: mm_recipients and postal_codes. The first one has a list of users in my system and the second one a list of 1 million postal codes in Canada.
My objective is to select a list of 50 users that live less than 10Km away from a central location and are between 25 and 50 years old.
The MM_RECIPIENTS table has the following fields:
The table POSTAL_CODES has the following fields:
Now, there is a calculation to come up with the distance, but even ignoring this fact for now, how would I get all the information from the two tables?
This is what I currently have (which by the way pretty much crashes the server...):
Any ideas to get this going?
SELECT * FROM mm_recipients, mm_recipients_lists, postal_codes
AND (recipient_bday_year < 1983 AND recipient_bday_year > 1958)
AND mm_recipients.postal_code = postal_codes.postal_code
ORDER BY rand()
Thanks a lot.