1

I have a MySQL database of contacts with three tables.

  1. person
  2. personContact
  3. personDetails

Each contact shares a primary key called 'ID'

The personContact table contains a value called 'personZip' which happens to be their mailing address zip code.

I'd like to write a SQL query that will give me all the contact data for each person in a specific array of zip codes.

I've written a simple statement to perform an inner join on 2 of the tables:

SELECT * FROM `personContact`
INNER JOIN person
ON personContact.ID=person.ID

I've written a statement to select only the zip codes I need:

SELECT * FROM 'personContact'
WHERE personContact.personZip=12564 
OR personContact.personZip=12563 
OR personContact.personZip=12522 
OR personContact.personZip=12590
OR personContact.personZip=12594
OR personContact.personZip=12533
OR personContact.personZip=12570
OR personContact.personZip=12589
OR personContact.personZip=10509

I'm not sure how to perform two joins, to merge all columns from all three tables.

I'm not sure how to write the query to accommodate both the selection of zip codes and the JOINS.

MySQL errors are not helping me move in the right direction.

3
  • 1
    Why don't use IN clause instead? Commented Oct 7, 2015 at 21:37
  • I posted exactly what you asked for right from the start. Have you even tried it? Commented Oct 7, 2015 at 22:05
  • @dbagley giving it a shot now. Thanks. Commented Oct 7, 2015 at 22:32

2 Answers 2

2

You were almost there. Use in which is equivalent to or.

SELECT pc.* --select columns from the other tables as needed. 
 FROM 
`personContact` pc 
INNER JOIN person p ON pc.ID = p.ID
INNER JOIN personDetails pd on pd.ID = p.ID
where pc.personzip in (12563, 12522, 10509) -- add more zips as needed
Sign up to request clarification or add additional context in comments.

6 Comments

pc.zip is throwing unknown column error. Not sure what .zip is.
it should have been personzip. modified the query
While this might work, I'm not sure what's happening here. pc.* does...what?
pc.* returns all columns from the personContact table only
This just filters zip codes and returns columns from personContact table only. Not seeing columns from other two tables joined.
|
1

Join all three tables and return all columns while filtering by zip code:

SELECT person.*, personContact.*, personDetails.*
FROM person
INNER JOIN personContact ON personContact.ID = person.ID
INNER JOIN personDetails ON personDetails.ID = person.ID
WHERE personContact.personZip = 12564
    OR personContact.personZip = 12563
    OR personContact.personZip = 12522
    OR personContact.personZip = 12590
    OR personContact.personZip = 12594
    OR personContact.personZip = 12533
    OR personContact.personZip = 12570
    OR personContact.personZip = 12589
    OR personContact.personZip = 10509

EDIT: Multiple OR statements can be replaced using IN as others have suggested

WHERE personContact.personZip IN (12564, 12563, 12522, 12590 ...)

4 Comments

Use IN would be more efficient in this case. select * would return all joined tables without describe them one by one.
I would probably also alias the tables, but I left it in the format presented in the question.
Any particular reason this query would take a very long time?
If you are comparing the run time to the other answer keep in mind that an additional table was added and more zip codes are being checked. Otherwise, there could be various factors such as number of rows, table index, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.