davidahines
davidahines

Reputation: 4094

SQL if join doesn't match then join something else

Poorly worded because I'm new to these operations in SQL, suppose I have a table with territories that are eastern and western with another table that has a field called address_state. I want to be able to join on the UCASE of address_state being equal to either the state_abbreviation OR the state_name

EG:

SELECT orders.total_value, territories.territory FROM orders 
INNER JOIN tblCanadianTerritories ON 
UCASE(orders.technical_address_state) = tblCanadianTerritories.state 
OR 
tblCanadianTerritories ON 
UCASE(orders.technical_address_state) = tblCanadianTerritories.name

WHERE UCASE(orders.technical_address_country) = "CANADA"

EDIT:

Have come to two possible solutions:

SELECT so_order.id, so_order.date_entered, so_order.check_if_new_customer, tblCanadianTerritories.territory, so_order.total_value  FROM so_order
INNER JOIN tblCanadianTerritories ON 
UCASE(so_order.technical_address_state) = tblCanadianTerritories.state   
OR
UCASE(so_order.technical_address_state) = tblCanadianTerritories.name
WHERE UCASE(so_order.technical_address_country) = "CANADA"
ORDER BY so_order.date_entered ASC

OR

SELECT so_order.id, so_order.date_entered, so_order.check_if_new_customer, 

tblCanadianTerritories.territory, so_order.total_value  FROM so_order
INNER JOIN tblCanadianTerritories ON UCASE(so_order.technical_address_state) = 

tblCanadianTerritories.state   
WHERE UCASE(so_order.technical_address_country) = "CANADA"
ORDER BY so_order.date_entered ASC
UNION 
SELECT so_order.id, so_order.date_entered,so_order.check_if_new_customer, 

tblCanadianTerritories.territory, so_order.total_value FROM so_order
INNER JOIN tblCanadianTerritories ON UCASE(so_order.technical_address_state) = 

tblCanadianTerritories.name
WHERE UCASE(so_order.technical_address_country) = "CANADA"
ORDER BY so_order.date_entered;

But they both seem to retrieve different results.

Upvotes: 2

Views: 1175

Answers (2)

Chandu
Chandu

Reputation: 82903

Try this:

SELECT orders.total_value, territories.territory 
    FROM orders AS so_order INNER JOIN tblCanadianTerritories 
        ON (
            UCASE(so_order.technical_address_state) = tblCanadianTerritories.state 
            OR 
            UCASE(so_order.technical_address_state) = tblCanadianTerritories.name
             )
WHERE UCASE(so_order.technical_address_country) = "CANADA"

If you want to use a UNION, try this:

SELECT orders.total_value, territories.territory 
    FROM orders AS so_order  INNER JOIN tblCanadianTerritories 
        ON UCASE(so_order.technical_address_state) = tblCanadianTerritories.state 
WHERE UCASE(so_order.technical_address_country) = "CANADA"
UNION
SELECT orders.total_value, territories.territory 
    FROM orders AS so_order  INNER JOIN tblCanadianTerritories 
        ON UCASE(so_order.technical_address_state) = tblCanadianTerritories.name 
WHERE UCASE(so_order.technical_address_country) = "CANADA"

Upvotes: 4

Paul Sasik
Paul Sasik

Reputation: 81429

You don't need a separate join. Try this:

SELECT o.total_value, territories.territory 
FROM orders AS o
INNER JOIN tblCanadianTerritories AS ct ON 
UCASE(o.technical_address_state) = ct.state 
OR 
UCASE(o.technical_address_state) = ct.name
WHERE UCASE(o.technical_address_country) = "CANADA"

(now with aliases)

Upvotes: 2

Related Questions