简体   繁体   English

SQL如果连接不匹配,则连接其他内容

[英]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. 措辞不佳是因为我对SQL中的这些操作是陌生的,假设我有一个表,该表具有东西方的领土,而另一个表具有名为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 我希望能够以等于state_abbreviation或state_name的address_state的UCASE身份加入

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. 但是他们似乎都获得了不同的结果。

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: 如果要使用UNION,请尝试以下操作:

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"

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) (现在带有别名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM