简体   繁体   English

在多行上进行AND的SQL查询

[英]Sql query for AND on multiple rows

I have a table (tblABC) which looks like 我有一张桌子(tblABC)看起来像

-----------------------------
BasicID   | Filter 1| Filter2 |
------------------------------
 100          1         2
 100          3         4
 101          8         9

I want to select the BasicID which has Filter1=1 and Filter2=4. 我想选择具有Filter1 = 1和Filter2 = 4的BasicID。 ie I want to get the output as 100 即我想得到的输出为100

I can't be using AND here as as it searches within the same row only. 我不能在这里使用AND,因为它仅在同一行中搜索。 ie select * from tblABC where Filter1=1 and Filter2=4 , yields no result. select * from tblABC where Filter1=1 and Filter2=4不会产生结果。

As of now the query that I use is 截至目前,我使用的查询是

select * from tblABC  
where 
and BasicID in 
(
select BasicID from tblABC  
where Filter1 IN (1) 
)
and BasicID  in 
(
select BasicID from tblABC  
where Filter2 IN (4) 
)

This one works for me. 这对我有用。 But there are like 12 Filter columns and when such a string search is run in large volumes, wouldn't it make the query slow. 但是有大约12个“过滤器”列,当这样的字符串搜索大量运行时,它不会使查询变慢。 What would be a more efficient way of doing this? 有什么更有效的方法?

I am using Microsoft SQL 2014 我正在使用Microsoft SQL 2014

Try below 试试下面

select basicid from tblABC where filter1 = 1 intersect 
select basicid from tblABC where filter1 = 4 

If the two filter values can be either in different rows or the same row, the GROUP BY / HAVING method will fail (in the same row case). 如果两个过滤器值可以位于不同的行或位于同一行,则GROUP BY / HAVING方法将失败(在同一行的情况下)。 This method will work in all cases (the intersect query by @Azar will work, too): 此方法在所有情况下均适用(@Azar的intersect查询也适用):

select distinct a.BasicID 
from tblABC as a
  join tblABC as b
    on a.BasicID = b.BasicID
where a.Filter1 = 1 
  and b.Filter2 = 4 ;

If you want the GROUP BY / HAVING COUNT method, this modification will work in all cases, too: 如果您要使用GROUP BY / HAVING COUNT方法,则此修改在所有情况下也都适用:

select basicid
from   tblABC
where  filter1 = 1
   or  filter2 = 4
group by
       basicid
having count(case when filter1 = 1 then 1 end) >= 1
   and count(case when filter2 = 4 then 1 end) >= 1 ;

Try this: 尝试这个:

SELECT BasicID
  FROM tblABC
  WHERE Filter1=1 OR Filter2=4
    GROUP BY BasicID
    HAVING COUNT(BasicID)=2

In case of 12 filters, change COUNT to be 12. 如果有12个过滤器,请将COUNT更改为12。

You can use group by and a count if you are sure one line doesn't match multiple rules (then the count might be off and the query breaks). 如果您确定一行不匹配多个规则,则可以使用group bycount (然后计数可能会关闭并且查询会中断)。

select basicid
from   tblABC
where  filter1 = 1
or     filter2 = 4
group
by     basicid
having count(*) = 2 /*number of filters*/

You can use the following query 您可以使用以下查询

SELECT DISTINCT BasicId 
FROM  tblABC  main
WHERE Filter1 = 1
    AND  EXISTS (SELECT TOP 1 Filter2
                 FROM tblABC
                 WHERE tblABC.BasicId =  main.BasicId
                   AND Filter2 = 4)

You have used the IN clause. 您已经使用了IN子句。 You can easily create three additional variants: using exists instead of in, using intersection, and using a selfjoin of tblABC. 您可以轻松地创建三个其他变体:使用exist而不是in,使用交集以及使用tblABC的自连接。

But as far as I know, query optimizers can translate automatically your query into these three variants and even it could get more non-trivial rewritings. 但据我所知,查询优化器可以将您的查询自动转换为这三个变体,甚至可以得到更多的平凡重写。

I suggest you: 我建议你:

  • First, let the optimizer work. 首先,让优化器工作。
  • Second, if your query is finally slow then create (if not exists) a index over BasicID. 其次,如果您的查询最终变慢,则在BasicID上创建(如果不存在)索引。
  • Third, if the query is still slow then use two bitmap indexes, over filter1 and over filter2. 第三,如果查询仍然很慢,则在filter1和filter2上使用两个位图索引。
  • And fourth if it is still slow use and indexed view. 第四,如果仍在缓慢使用和建立索引视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM