【问题标题】:Access SQL select count number of specific field from one table join another tableAccess SQL select count number of specific field from a table join another table
【发布时间】:2016-05-08 15:59:23
【问题描述】:

我的访问文件中有这两个表(我让它变得简单) 表 1:用户:

ID        User_Code       User_Name
==        =========       ==========
1         1111            John
2         2222            Alex
3         3333            Tom

表 2 GB:

ID        User_Code       First(is Boolean)
==        =========       =================
1         1111            Yes
2         2222            Yes
3         1111            Yes
4         1111            Yes

我想要一个结果如下表的 SQL 查询:

User_Name       CountNum
=========       =========
John            3
Alex            1
Tom             0

我知道我必须使用内连接和 distinct 和 count 函数,但不知道具体如何??? 感谢您的回答。

【问题讨论】:

    标签: sql ms-access


    【解决方案1】:

    尚不清楚distinct 的来源。这只是joingroup by

    select u.user_name, count(gb.user_code)
    from users as u left join
         gb
         on u.user_code = gb.user_code
    group by u.user_name;
    

    注意:user_code 的用户 join 是可疑的。 Users 表有一个id。通常用于join

    【讨论】:

      【解决方案2】:

      你想要左连接而不是内连接,并且不需要 distinct...

      select u.user_name, count(g.user_code)
      from users u left join gb g
           on u.user_code = g.user_code
      group by u.user_name
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-12
        • 2015-12-10
        • 1970-01-01
        • 2011-04-06
        • 2021-07-21
        • 1970-01-01
        • 2018-05-13
        • 2014-12-02
        相关资源
        最近更新 更多