ACCESS SQL查询

时间:2012-07-14 07:48:47

标签: ms-access-2010

表1

GenericID RelatedGenericID
--------- ----------------
25            1566

220           1570

330           1571

表2

GenericID GenericName
--------- -----------
25           a

220          b

330          c

1566         d

1570         e

1571         f

我希望MSaccess查询的结果为:

GenericID GenericName RelatedGenericID  RelatedGenericName
--------  ----------- ----------------  ------------------
25           a          1566               d

220          b          1570               e

330          c          1571               f
任何人都可以帮助我。感谢

1 个答案:

答案 0 :(得分:0)

这是仅使用MS Access中的查询设计窗口构建的。包含名称的G2已经两次添加到设计网格中。

这是它在MS Access 2010 SQL视图中的外观:

SELECT g1.GenericID, g2.GenericName, g2_1.GenericName, g1.RelatedGenericID 
FROM (g1 INNER JOIN g2 ON g1.GenericID = g2.GenericID) INNER JOIN g2 AS g2_1 ON g1.RelatedGenericID = g2_1.GenericID;

整理:

SELECT g1.genericid,
       g2.genericname,
       g1.relatedgenericid,
       g2_1.genericname
FROM   (g1
        INNER JOIN g2
                ON g1.genericid = g2.genericid)
       INNER JOIN g2 AS g2_1
               ON g1.relatedgenericid = g2_1.genericid;