1

有人可以帮助我吗,我必须比较两个表,我的经理想查看 Access 数据库中两个表之间的比较。两个表都包含 IP 电话、它们的类型、MAC 和分配给它们的站点 ID。我的经理想要以一种形式查看这些数据。我知道我可以用两个子表单来做到这一点,但是如果我可以用一个 sql 语句来做到这一点会更好,因为我知道它可以做到,但我只是愚蠢地这样做。我需要的是三列,第 1 列 = 手机类型,第 2 列 = 表 1 计数,第 3 列 = 表 2 计数:

Handset Type |TABLE 1 COUNT| TABLE 2 COUNT|
CISCO7911    | 100         | 50
CISCO7942    | 100         | 50

我目前有这个只通过查询一个表来处理其中两个列,但是我将如何添加最后一列,我已经尝试过 UNION 但这会将数据添加到额外的行而不是另一列。

SELECT tbl_handsets.handset_type,
    Count(IIf(handset_site_id='12345',1,Null)) AS myCompany_Number
FROM tbl_Handsets
GROUP BY tbl_handsets.handset_type

有任何想法吗???

4

2 回答 2

1

如果您告诉我们此查询将返回您想要的数据Handset Type并且TABLE 1 COUNT...

SELECT
    handset_type,
    Count(IIf(handset_site_id='12345',1,Null)) AS myCompany_Number
FROM tbl_Handsets
GROUP BY handset_type

...并替换tbl_Accenture为返回您想要的内容tbl_Handsets和,然后构建一个使用这两个作为子查询的查询并将它们连接在一起。FROMHandset TypeTABLE 2 COUNT

SELECT
    t1.handset_type,
    t1.myCompany_Number AS [TABLE 1 COUNT],
    t2.myCompany_Number AS [TABLE 2 COUNT]
FROM
    (
        SELECT
            handset_type,
            Count(IIf(handset_site_id='12345',1,Null))
                AS myCompany_Number
        FROM tbl_Handsets
        GROUP BY handset_type
    ) AS t1
    INNER JOIN
    (
        SELECT
            handset_type,
            Count(IIf(handset_site_id='12345',1,Null))
                AS myCompany_Number
        FROM tbl_Accenture
        GROUP BY handset_type
    ) AS t2
    ON t1.handset_type = t2.handset_type
于 2012-07-25T13:29:35.703 回答
0

可能是它可以提供帮助,但我努力测试它。

如果您的表有一个代表phone_type_id的列,那么查询将更改为

SELECT tbl_handsets.handset_type,
    COUNT(*) AS Table1_Count,
    ( SELECT COUNT(*) FROM tbl_Accenture 
        WHERE tbl_Accenture.handset_type = tbl_handsets.handset_type
        AND  tbl_Accenture.handset_site_id  = '15017' ) 
    AS Table2_Count  
FROM tbl_Handsets
Where tbl_handsets.handset_site_id = '15017'
GROUP BY tbl_handsets.handset_type
于 2012-07-25T09:39:33.713 回答