找到一种方法来查询使用SQL满足所有条件的项目列表

时间:2011-03-18 13:13:30

标签: sql sql-server tsql

我正在尝试执行以下操作:

select i.FirstName, i.LastName, COUNT(1) from (
    select u.Id, uw.WidgetId from [DB].[dbo].[Widgets] w inner join
     UserWidgets uw on w.Id = uw.WidgetId inner join
     Users u on uw.UserId = u.Id
     where uw.WidgetId in ('29017318-FD89-4952-A3A2-8405BD5C5C44', 
                           'BDB7D25C-0794-4965-842D-E6D03A250418',
                          'CB4553AC-A47B-4AA6-9231-5C59C8F97655')
     group by u.Id, uw.WidgetId
     ) a
 inner join [Db2].[dbo].[Identities] i on a.Id = i.IdentityId
 group by i.LastName, i.FirstName
 order by i.LastName, i.FirstName

我想要的是确保“In”语句要求用户只有那3个ID。不多也不少。

这样做的最佳方式是什么?

3 个答案:

答案 0 :(得分:3)

尝试:

select i.FirstName, i.LastName, COUNT(1) from (
    select u.Id, uw.WidgetId from [DB].[dbo].[Widgets] w inner join
     UserWidgets uw on w.Id = uw.WidgetId 
                    and uw.WidgetId in ('29017318-FD89-4952-A3A2-8405BD5C5C44', 
                           'BDB7D25C-0794-4965-842D-E6D03A250418',
                          'CB4553AC-A47B-4AA6-9231-5C59C8F97655')
     inner join Users u on uw.UserId = u.Id
     left join UserWidgets uw2 on uw2.userid = u.id
                    and uw2.WidgetId not in ('29017318-FD89-4952-A3A2-8405BD5C5C44', 
                           'BDB7D25C-0794-4965-842D-E6D03A250418',
                          'CB4553AC-A47B-4AA6-9231-5C59C8F97655')
     where uw2.widgetid is null
     group by u.Id, uw.WidgetId
     ) a
 inner join [Db2].[dbo].[Identities] i on a.Id = i.IdentityId
 group by i.LastName, i.FirstName
 having count(1) = 3
 order by i.LastName, i.FirstName

答案 1 :(得分:2)

    select i.FirstName, i.LastName, COUNT(1) from (
    select u.Id, uw.WidgetId from [DB].[dbo].[Widgets] w inner join
     UserWidgets uw on w.Id = uw.WidgetId inner join
     Users u on uw.UserId = u.Id
     where uw.WidgetId in ('29017318-FD89-4952-A3A2-8405BD5C5C44', 
                           'BDB7D25C-0794-4965-842D-E6D03A250418',
                          'CB4553AC-A47B-4AA6-9231-5C59C8F97655')
     AND count(WidgetId) = 3
     group by u.Id, uw.WidgetId
     ) a
 inner join [Db2].[dbo].[Identities] i on a.Id = i.IdentityId
 group by i.LastName, i.FirstName
 order by i.LastName, i.FirstName

我在查询中添加了'AND count(WidgetId)= 3'...我相信这会有效吗?

答案 2 :(得分:1)

这应该有效:

SELECT i.firstname,
       i.lastname,
       COUNT(1)
FROM   (SELECT u.id,
               uw.widgetid
        FROM   [DB].[dbo].[Widgets] w
               INNER JOIN userwidgets uw
                 ON w.id = uw.widgetid
               INNER JOIN users u
                 ON uw.userid = u.id
        WHERE  uw.widgetid IN ( '29017318-FD89-4952-A3A2-8405BD5C5C44',
                                        'BDB7D25C-0794-4965-842D-E6D03A250418'
                                        ,
                                'CB4553AC-A47B-4AA6-9231-5C59C8F97655' )
               AND NOT EXISTS (SELECT *
                               FROM   [DB].[dbo].[Widgets] w2
                                      INNER JOIN userwidgets uw2
                                        ON w2.id = uw2.id
                               WHERE  w2.id = w.id
                                      AND uw2.widgetid NOT IN (
                              '29017318-FD89-4952-A3A2-8405BD5C5C44',
                              'BDB7D25C-0794-4965-842D-E6D03A250418'
                              ,
                              'CB4553AC-A47B-4AA6-9231-5C59C8F97655' ))
        GROUP  BY u.id,
                  uw.widgetid) a
        HAVING COUNT(DISTINCT uw.widgetid) = 3
       INNER JOIN [Db2].[dbo].[Identities] i
         ON a.id = i.identityid
GROUP  BY i.lastname,
          i.firstname
ORDER  BY i.lastname,
          i.firstname