在三个字段中显示国家/地区的名称

时间:2014-05-31 23:38:04

标签: ms-access-2010

我有一个名为 Principal 的表格,其中包含以下列:

  • PERSON_ID
  • Country1_ID
  • Country2_ID
  • Country3_ID

我还有一个名为国家/地区的表,其中包含以下列:

  • ID
  • COUNTRY_NAME

我需要制作一个选择,显示Person_ID和三个国家的名称。 我试过INNER JOIN,但总是给我三个领域的同一个国家。

SELECT Principal.Person_ID, Countries.Country_Name AS Country1, Countries.Country_Name AS Country2, Countries.Country_Name AS Country3 
FROM
    (Countries
    INNER JOIN Principal
    ON Countries.ID = Principal.Country1_ID OR Countries.ID = Principal.Country2_ID  OR Countries.ID = Principal.Country3_ID);

什么是正确的SELECT?

1 个答案:

答案 0 :(得分:1)

我认为这会让你想要你想要的。当国家/地区不匹配时,它会返回空白。

SELECT Principal.Person_ID
,(SELECT ISNull(Countries.Country_Name,'') FROM Countries WHERE Principal.Country1_ID OR Countries.ID ) AS Country1
,(SELECT ISNull(Countries.Country_Name,'') FROM Countries WHERE Principal.Country2_ID OR Countries.ID ) AS Country2
,(SELECT ISNull(Countries.Country_Name,'') FROM Countries WHERE Principal.Country3_ID OR Countries.ID ) AS Country3;

合并可能会更好,但我对它们并不熟悉。