简体   繁体   English

如果第一个条件为true,则停止从SQL查询的where子句中的其他条件获取数据

[英]If first condition is true then stop fetching from the other conditions in where clause of a SQL query

Query: 查询:

where  (table1.subject_1 like '%TEST1%'  OR 
        table1.subject_1 like '%TEST2%'  OR 
        table1.subject_1 like '%TEST3%'  OR 
        table1.subject_1 like '%TEST4%'
       ) 
       OR 
       (table1.subject_2 like '%TEST1%'  OR 
        table1.subject_2 like '%TEST2%'  OR 
        table1.subject_2 like '%TEST3%'  OR 
        table1.subject_2 like '%TEST4%'
       )

Here if subject_1 = TEST1 then no need to search for the remaining conditions, if not found then search for the other conditions. 在这里, if subject_1 = TEST1则无需搜索其余条件,如果找不到,则搜索其他条件。

I need a record having either of subject_1 from the above query. 我需要一条包含上述查询中subject_1的记录。 If subject_1 does not match with any of the results then search for subject_2 . If subject_1与任何结果都不匹配,则搜索subject_2

My problem: from the above query, multiple records are being returned where subject_1 matches TEST1 and TEST2 both. 我的问题:从上述查询中,返回多个记录,其中subject_1TEST1TEST2都匹配。

Example: 例:

no,  name,     add1,    occ,   date,   subject_1,subject_2,Exclusion_number        
-----------------------------------------------------------------------------
446 REBECCA   street1    Y    1/1/2001   TEST1   AB               10
446 REBECCA   street1    Y    1/1/2001   TEST2   A                11

I should be able to fetch one row as subject_1 like '%TEST1%' match found. 我应该能够以subject_1 like '%TEST1%'获取一行, subject_1 like '%TEST1%'匹配项。 I should not get the second row, as the first condition satisfied already. 我不应该得到第二行,因为第一个条件已经满足。

Currently with my query, I am getting 2 rows, where the requirement is to get only one row. 当前与我的查询,我得到2行,要求只得到一行。

In case first condition fails then I should check the second condition subject_2 like '%TEST2%'. 万一第一个条件失败,那么我应该检查第二个条件subject_2,例如'%TEST2%'。

This will return the first row that matches any of the criteria. 这将返回与任何条件匹配的第一行。

SELECT TOP 1 *
FROM TABLE1
where  
(table1.subject_1 like '%TEST1%' OR table1.subject_1 like '%TEST2%' OR 
table1.subject_1 like '%TEST3%' OR table1.subject_1 like '%TEST4%') OR 
(table1.subject_2 like '%TEST1%' OR table1.subject_2 like '%TEST2%' OR 
table1.subject_2 like '%TEST3%' OR table1.subject_2 like '%TEST4%')

OR is completely associative, and is short-circuited (given A or B , if A is true, then B is never checked) so your parentheses don't do anything, given (A or B) or (C or D) , if A is true, then (A or B) is automatically true, then the entire expression is true, and the row is returned. OR是完全相关的,并且是短路(给A or B ,如果A是真的,那么B是从来没有检查),这样你的括号没有做任何事情,因为(A or B) or (C or D)如果A为真,然后(A or B)自动为真,然后整个表达式为真,并返回该行。

In a case like A or B or C or D , if A is true, the row is returned, if A is false but B is true, the row is returned, if A and B are false but C is true, the row is returned, etc. A or B or C or D类的情况下,如果A为true,则返回该行;如果A为false,但B为true,则返回该行;如果AB为false,但C为true,则该行为返回等

Hope that helps. 希望能有所帮助。

EDIT: 编辑:

In some cases, the query optimizer may choose not the short circuit the OR (so if the 2nd predicate might throw an exception it may sometimes be executed first): (see also: OR Operator Short-circuit in SQL Server ), but for the OP's case, associative property still determines evaluation of the expression. 在某些情况下,查询优化器可能不会选择短路OR(因此,如果第二个谓词可能引发异常,则有时可能会先执行它):(另请参见: SQL Server中的OR运算符短路 ),但对于在OP的情况下,关联属性仍然决定表达式的评估。

You probably want to restrict the result set based on the first comparison that succeeds. 您可能希望基于成功的第一个比较来限制结果集。 But, as already said in some of the comments, you can't assure which condition will be evaluated first and returned, unless there is a predefined ordering. 但是,正如某些评论中已经说过的那样,除非有预定义的顺序,否则您无法确保首先评估哪个条件并返回该条件。

So, I suppose you could define and assign an ordering based on your priority and then use ROWNUM = 1 to get the first row matched in the order. 因此,我想您可以根据优先级定义和分配顺序,然后使用ROWNUM = 1获取该顺序中匹配的第一行。

SELECT *
FROM (
    SELECT t.*
    FROM Table1 t
    ORDER BY CASE 
            WHEN subject_1 LIKE '%TEST1%'
                THEN 1
            WHEN subject_1 LIKE '%TEST2%'
                THEN 2
            WHEN subject_1 LIKE '%TEST3%'
                THEN 3
            END
        ,CASE 
            WHEN subject_2 LIKE '%TEST1%'
                THEN 4
            WHEN subject_2 LIKE '%TEST2%'
                THEN 5
            WHEN subject_2 LIKE '%TEST3%'
                THEN 6
            END
    )
WHERE rownum = 1

Demo 演示版

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

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