I am trying to display customers who live in the same city as William
. This query gets his zip code and finds any customers with his zip code. I need to find the customers in the city NAME based from the zip code. There are a few zip codes per city Name.
Table city:
zipCode (PK) cityName, stateCode
Table customer:
customerId (PK) customerName, customerAddress, zipCode (FK)
My query so far...
SELECT
cu2.customerName AS 'Customer Name',
ci2.cityName AS 'City Name'
FROM
customer as cu
INNER JOIN
city as ci ON cu.zipCode = ci.zipCode
INNER JOIN
city as ci2 ON ci.cityName = ci2.cityName
INNER JOIN
customer as cu2 ON ci2.zipCode = cu2.zipCode
AND cu2.customerName <> cu.customerName
WHERE
cu2.customerName = 'William'
It shows no results because I think it is only checking for William's zip code when the city he is in has two other zip codes. His is 91709 and the cityNAME has in addition 91708 and 91710. I need to get the other customers within that cityNAME.