MS访问多查询左连接

Posted

技术标签:

【中文标题】MS访问多查询左连接【英文标题】:MS access multiple query left join 【发布时间】:2014-06-07 07:26:31 【问题描述】:

我正在使用多个子查询在 MS ACCESS 中创建一个表,其中第一个表包含整个集合,其余表基于另一个条件 - 这是我的代码

    SELECT a.id, 
           a.cnt AS Total_Count, 
           b.cnt AS Income_Count 
    INTO   data 
    FROM   (SELECT id, 
                   Count(*) AS Cnt 
            FROM   test 
            GROUP  BY id) AS a 
           LEFT JOIN (SELECT id, 
                             Count(*) AS Cnt 
                      FROM   test 
                      WHERE  inc > 25 
                      GROUP  BY id) AS b 
                  ON a.id = b.id; 

It works fine. But when I am putting another sub query its not working 

SELECT a.id, 
       a.cnt AS Total_Count, 
       b, 
       b.cnt AS Income_Count, 
       c.cnt AS EXP_Count 
INTO   data 
FROM   (SELECT id, 
               Count(*) AS Cnt 
        FROM   test 
        GROUP  BY id) AS a 
       LEFT JOIN (SELECT id, 
                         Count(*) AS Cnt 
                  FROM   test 
                  WHERE  inc > 25 
                  GROUP  BY id) AS b 
              ON a.id = b.id 
       LEFT JOIN (SELECT id, 
                         Count(*) AS Cnt 
                  FROM   test 
                  WHERE  exp > 25 
                  GROUP  BY id) AS c 
              ON a.id = c.id; 

你能帮我解决这个问题吗?

【问题讨论】:

“不工作”是什么意思?空集?您的表格中的值是什么?你期望得到什么结果? 【参考方案1】:
SELECT a.id, 
       a.cnt AS Total_Count, 
       b,  <----------------- What is this? b is an alias for a table, not a field  
       b.cnt AS Income_Count, 
       c.cnt AS EXP_Count 
INTO   data 
FROM   (SELECT id, 
               Count(*) AS Cnt 
        FROM   test 
        GROUP  BY id) AS a 
       LEFT JOIN (SELECT id, 
                         Count(*) AS Cnt 
                  FROM   test 
                  WHERE  inc > 25 
                  GROUP  BY id) AS b 
              ON a.id = b.id 
       LEFT JOIN (SELECT id, 
                         Count(*) AS Cnt 
                  FROM   test 
                  WHERE  exp > 25 
                  GROUP  BY id) AS c 
              ON a.id = c.id; 

删除“b”或将其替换为实际字段。请注意,如果您使用 b.id,那么您将在尝试创建具有相同名称“id”的两列时遇到问题,您将不得不更改其中一列的名称。我在 sql server 上运行了它,没有'b'它运行良好。

【讨论】:

以上是关于MS访问多查询左连接的主要内容,如果未能解决你的问题,请参考以下文章

左连接 SQL 查询 - MS Access

mysql vs ms-access DB上的sql查询左连接

MS Access 多连接查询

MS Access 多连接查询

访问为空的左连接成员时的教义痛苦

ms-access 中的左连接和内连接