SQL选择条件问题

时间:2009-12-18 18:32:54

标签: sql sql-server tsql sql-match-all

我对选择语句条件有一个简短的问题。

我的下表包含以下项目。我需要得到的是与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 个答案:

答案 0 :(得分: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 

请注意确保传递值时行为如何工作,但这是一个开始。

答案 1 :(得分:3)

我从@Cade Roux那里得到了答案,我就是这样做的。

但是FWIW,这是另一种解决方案:

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

假设TypeIdObjectId的唯一性。


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

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

答案 2 :(得分: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;

要添加其他TypeIds,您只需添加另一个联接:

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;

随着类型数量的增加,他的做法变得更好。

答案 3 :(得分: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