【问题标题】:Querying tables with count from other tables从其他表中查询具有计数的表
【发布时间】:2016-04-20 12:24:35
【问题描述】:

我在数据库中有三个表,它们是“UserGroup”,它有一个唯一的用户组。第二个表是“用户”,它有用户,但多个用户可以属于同一个用户组。

同样,我有第三个表“角色”,其中包含角色和多个角色 可以属于同一个“用户组”。

因此,父表是 UserGroup,该表的 id 是另外两个表中的外键。

这里已经提出了一个相关的问题,但如果你能用我的方案回答,那就更好了 Select from one table with count from two other tables

我正在尝试查询这些表,以便我需要从 这两个表中的每一个,即来自“用户”和角色“。如果您可以帮助解决 Linq 查询,我们将不胜感激。

例如:

“用户组表”

Id GroupName
1   Admin
2   IT
3   Helpdesk

“用户表”

Id USerGroupId UserName
1    1          Tom
2    1          Mickey
3    2          John
4    3          Sandy

“角色表”

Id  USerGroupId    Role
1     2            Supervisor 
2     2            Superidetendent
3     3            etc
4     3            etc

输出应该显示:

GroupName     USerCount    RolesCount
Admin           2            0
IT              1            2
Helpdesk        1            2

【问题讨论】:

    标签: sql-server database linq sql-server-2008


    【解决方案1】:
    SELECT GroupName 
    ,(SELECT COUNT*) FROM Users U WHERE U.USerGroupId=G.Id) [USerCount]   
    ,(SELECT COUNT*) FROM Roles r WHERE R.USerGroupId=G.Id) [RolesCount]  
    FROM Group g
    

    【讨论】:

    • 你犯了和我一样的错误... LINQ 查询。
    • 您好,上面的查询是正确的。我可以得到它的 linq 版本吗?
    【解决方案2】:

    我认为这样的事情应该可行:

    UsersTable.GroupBy(u => u.USerGroupId).Select(group => new { 
                                 USerGroupId = group.Key, 
                                 Count = group.Count() 
                            }).OrderBy(x => x.USerGroupId);
    
    RolesTable.GroupBy(u => u.USerGroupId).Select(group => new { 
                                 USerGroupId = group.Key, 
                                 Count = group.Count() 
                            }).OrderBy(x => x.USerGroupId);
    

    【讨论】:

      【解决方案3】:

      这是 c# 中的 linq to Entity(如果你使用 Entity Framework 连接到 DB)

      var query = 
          from h in (
              (from b in db.UserTable
              group b.UserGroup by new {
                b.UserGroup.GroupName
              } into g
              select new {
                g.Key.GroupName,
                UserCount = g.Count(p => p.GroupName != null)
              }))
          join k in (
              (from d in db.Roles
              group d.UserGroup by new {
                d.UserGroup.GroupName
              } into g
              select new {
                g.Key.GroupName,
                RolesCount = g.Count(p => p.GroupName != null)
              })) on h.GroupName equals k.GroupName into k_join
          from k in k_join.DefaultIfEmpty()
          select new {
            h.GroupName,
            h.UserCount,
            RolesCount = k.RolesCount
          };
      

      这是 c# 中的 linq to SQL(如果您使用 DBML)

      var query = 
              from h in (
                  (from a in db.UserGroup
                  join b in db.UserTable on new { Id = a.Id } equals new { Id = b.UserGroupId } into b_join
                  from b in b_join.DefaultIfEmpty()
                  group a by new {
                    a.GroupName
                  } into g
                  select new {
                    g.Key.GroupName,
                    UserCount = g.Count(p => p.GroupName != null)
                  }))
              join k in (
                  (from d in db.Roles
                  group d.UserGroup by new {
                    d.UserGroup.GroupName
                  } into g
                  select new {
                    g.Key.GroupName,
                    RolesCount = g.Count(p => p.GroupName != null)
                  })) on h.GroupName equals k.GroupName into k_join
              from k in k_join.DefaultIfEmpty()
              select new {
                h.GroupName,
                h.UserCount,
                RolesCount = k.RolesCount
              };
      

      这是源 SQL

      SELECT h.GroupName, h.UserCount, k.RolesCount
      FROM
      (SELECT a.GroupName, count(a.GroupName) as UserCount
      from UserGroup a
      LEFT JOIN UserTable b on a.id = b.UserGroupId
      group by a.GroupName) h
      LEFT JOIN (
      SELECT a.GroupName, count(a.GroupName) as RolesCount
      from UserGroup a
      INNER JOIN Roles d on a.id = d.UserGroupId
      group by a.GroupName) k on h.GroupName = k.GroupName
      

      【讨论】:

      • 我已经检查了查询,但它没有返回正确的结果,因为 admin 组,此查询返回 2 为 usercount 和 rolescount。对于角色计数,它应该返回 0。请您再帮忙解决一下。
      • @Sweetie 我编辑并更正了我的答案。你能试试吗
      • @Sweetie 如果我帮助了你,你能接受我的回答吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2021-12-19
      相关资源
      最近更新 更多