I'm trying to join three tables in order to generate a list of all our users and their locations.
The first table lists all of our users with two separate IDs--userId and uid. The second table is an "orders" table where user location data (delivery zone and billing zone) is stored by uid, and we have a third table that acts as a location key (e.g., zone_id 18 = zone_name Florida).
What I want is each userId, plus the converted human-readable zone_name for each user's billing zone and delivery zone.
My code works fine when I try only to convert one of these zones. So this query works great:
SELECT u.userId, o.delivery_postal_code, o.billing_postal_code, z1.zone_name
FROM webapp.users AS u
LEFT OUTER JOIN practice_drupal.uc_orders AS o
ON u.uid = o.uid
LEFT OUTER JOIN practice_drupal.uc_zones AS z1
ON o.billing_zone = z1.zone_id;
But I get an error when I try to generate the zone_name for the second zone (delivery). The error is "Unknown column 'z2.zone_name' in 'field list' ". Here's my query:
SELECT u.userId, o.delivery_postal_code, z1.zone_name, o.billing_postal_code, z2.zone_name
FROM webapp.users AS u
LEFT OUTER JOIN practice_drupal.uc_orders AS o
ON u.uid = o.uid
LEFT OUTER JOIN practice_drupal.uc_zones AS z1
ON o.billing_zone = z1.zone_id;
LEFT OUTER JOIN practice_drupal.uc_zones AS z2
ON o.delivery_zone = z2.zone_id;
I'm confused because my scenario seems almost identical to this one, with the exception of using outer joins, but I can't get bluefeet's solution to work for me: SQL Inner-join with 3 tables?
Any ideas? Thanks!