2011-04-12 81 views
2

措辭不好,因爲我對SQL中的這些操作不太熟悉,假設我有一個表的東西分區,另一個表中有一個名爲address_state的表。我希望能夠加入對ADDRESS_STATE的UCASE等於要麼STATE_ABBREVIATION或STATE_NAMESQL如果連接不匹配然後加入其他東西

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" 

編輯:

既然來了兩個可能的解決方案:

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; 

但他們都似乎檢索不同的結果。

+0

你可能有一個錯誤。您正在使用名爲tblCana​​dianTerritories的表/視圖加入名爲orders的表/視圖。然後你引用一個名爲so_order的表/視圖。訂單和訂單是錯誤的嗎? – 2011-04-12 17:24:44

+0

是的,消毒。 – davidahines 2011-04-12 17:49:46

回答

4

試試這個:

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" 

如果你想使用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" 
+0

工會選擇會做同樣的事情嗎? – davidahines 2011-04-12 17:19:34

+1

@dah:是的,我使用UNION查詢更新了這篇文章。 – Chandu 2011-04-12 17:21:35

+1

@dah在瑣碎的情況下是(如在原始問題中),但是如果您的陳述變得更加複雜,則否否 – 2011-04-12 17:22:00

2

你並不需要一個單獨的連接。試試這個:

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" 

(現在的別名)

+0

我希望我能選擇兩個答案,這個答案也很棒。有一艘快艇。 – davidahines 2011-04-12 17:43:26

相關問題