我有一个名为 Principal 的表格,其中包含以下列:
我还有一个名为国家/地区的表,其中包含以下列:
我需要制作一个选择,显示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?
答案 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;
合并可能会更好,但我对它们并不熟悉。