映射表MySQL / Access

时间:2012-06-19 12:57:52

标签: mysql ms-access

我有一个简短的访问/ mySQL问题。我有一个下面格式的映射表。

ID      Category_A      Category_B      Category_C      Team
 1              a               b                         T1
 2              a                               d         T2

我有第二个表,其中还包括Category_A,Category_B和Category_C。我想根据映射表将Team值加入到我的第二个表中。我的问题是当有一个空白(例如ID = 2,Category_B)时,映射应该将T2分配给包含Category_A = a和Category_C = d的任何行,而不管Category_B中的值。

可以完成这种类型的映射吗?

感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

在MS Access中,我认为你需要的是:

SELECT t.ID, m.Team
FROM Team t
 INNER JOIN Mapping m
      ON (m.Category_C = t.Category_C) 
     AND (m.Category_B = t.Category_B) 
     AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Not Null
     AND m.Category_B Is Not Null 
     AND m.Category_A Is Not Null

UNION ALL
SELECT t.ID, m.Team
FROM Team t
 INNER JOIN Mapping m
      ON  (m.Category_B = t.Category_B) 
     AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Null
     AND m.Category_B Is Not Null 
     AND m.Category_A Is Not Null

UNION ALL
SELECT t.ID, m.Team
FROM Team t
 INNER JOIN Mapping m
      ON (m.Category_C = t.Category_C)
     AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Not Null
     AND m.Category_B Is Null 
     AND m.Category_A Is Not Null

UNION ALL
SELECT t.ID, m.Team
FROM Team t
 INNER JOIN Mapping m
      ON (m.Category_C = t.Category_C) 
     AND (m.Category_B = t.Category_B) 
WHERE m.Category_C Is Not Null
     AND m.Category_B Is Not Null 
     AND m.Category_A Is Null