从其他表

时间:2016-04-20 12:24:35

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

我在数据库中有三个表是" UserGroup"它有一个独特的用户组。第二个表是"用户"具有用户但多个用户的用户可以属于同一用户组。

同样,我有第三张桌子"角色"包含角色和多个角色 可以属于同一个" 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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我觉得这样的事情应该有效:

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);

答案 2 :(得分:0)

这是c#中的实体的linq(如果使用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