1

我有一个关于 select 语句条件的快速问题。

我有下表,其中包含以下项目。我需要得到的是与两种类型 id 匹配的对象 id。

TypeId  ObjectId
1       10
2       10
1       11

所以我需要同时获取对象 10,因为它匹配类型 id 1 和 2。

SELECT ObjectId
FROM Table
WHERE TypeId = 1
AND TypeId = 2

显然这不起作用,因为它不会匹配同一行的两个条件。如何执行此查询?另请注意,我可能会传入 2 个或更多类型 ID 以缩小结果范围。

4

4 回答 4

5

自加入:

SELECT t1.ObjectId 
FROM Table AS t1
INNER JOIN Table AS t2
    ON t1.ObjectId = t2.ObjectId
    AND t1.TypeId = 1 
    AND t2.TypeId = 2 

请注意在传递值时您希望行为如何工作,但这是一个开始。

于 2009-12-18T18:36:00.647 回答
3

我赞成@Cade Roux 的回答,我就是这样做的。

但是FWIW,这是一个替代解决方案:

SELECT ObjectId
FROM Table
WHERE TypeId IN (1, 2)
GROUP BY ObjectId
HAVING COUNT(*) = 2;

假设唯一性超过TypeId, ObjectId


重新评论@Josh,他可能需要搜索三个或更多TypeId值:

使用的解决方案JOIN需要您正在搜索的每个值的连接。GROUP BY如果您发现自己正在搜索越来越多的值,则使用上述解决方案可能会更容易。

于 2009-12-18T18:49:53.973 回答
1

这段代码是在考虑 Oracle 的情况下编写的。对于其他风格的 SQL,它应该足够通用

select t1.ObjectId from Table t1
join Table t2 on t2.TypeId = 2 and t1.ObjectId = t2.ObjectId
where t1.TypeId = 1;

要添加额外的 TypeId,您只需添加另一个联接:

select t1.ObjectId from Table t1
join Table t2 on t2.TypeId = 2 and t1.ObjectId = t2.ObjectId
join Table t3 on t3.TypeId = 3 and t1.ObjectId = t3.ObjectId
join Table t4 on t4.TypeId = 4 and t1.ObjectId = t4.ObjectId
where t1.TypeId = 1;

重要提示:当您添加更多连接时,性能会受到很大影响。

关于比尔的回答,您可以将其更改为以下内容,以摆脱假设唯一性的需要:

SELECT ObjectId
FROM (SELECT distinct ObjectId, TypeId from Table)
WHERE TypeId IN (1, 2)
GROUP BY ObjectId
HAVING COUNT(*) = 2;

随着类型数量的增加,他的做法可以更好地扩展。

于 2009-12-18T18:34:44.627 回答
0

尝试这个

样本输入:(案例 1)

declare @t table(Typeid int,ObjectId int)
insert into @t 
    select 1,10 union all select 2,10  union all    
    select 1,11 
select * from @t 

样本输入:(案例 2)

declare @t table(Typeid int,ObjectId int)
insert into @t 
    select 1,10 union all select 2,10  union all 
    select 3,10 union all select 4,10  union all 
    select 5,10 union all select 6,10  union all 
    select 1,11 union all select 2,11  union all 
    select 3,11 union all select 4,11  union all 
    select 5,11 union all select 1,12  union all 
    select 2,12  union all select 3,12 union all 
    select 4,12  union all select 5,12 union all 
    select 6,12  
select * from @t

示例输入:(案例 3)[有重复条目]

declare @t table(Typeid int,ObjectId int)
insert into @t 
    select 1,10 union all select 2,10  union all 
    select 1,10 union all select 2,10 union all
    select 3,10 union all select 4,10  union all 
    select 5,10 union all select 6,10  union all 
    select 1,11 union all select 2,11  union all 
    select 3,11 union all select 4,11  union all 
    select 5,11 union all select 1,12  union all 
    select 2,12  union all select 3,12 union all 
    select 4,12  union all select 5,12 union all 
    select 6,12  union all select 3,12 

对于案例 1,输出应为 10

对于案例 2 和 3,输出应为 10 和 12

询问:

select X.ObjectId from 
(
select 
            T.ObjectId
            ,count(ObjectId) cnt
from(select distinct ObjectId,Typeid from @t)T
where T.Typeid in(select Typeid from @t)
group by T.ObjectId )X
join (select max(Typeid) maxcnt from @t)Y
on X.cnt = Y.maxcnt
于 2009-12-19T03:56:20.537 回答