How to find out how many OR conditions are met?

How can I find out which of my results satisfied how many conditions /

SELECT 
    [TITLE] 
FROM 
    [M_TIPS] 
WHERE 
    [TITLE] LIKE '%VALUE%' OR 
    [TITLE] LIKE '%SQL%'; 

How to find out which of the results fulfilled only 1 OR condition, and which result satisfied both conditions. The number of conditions is not static; it may increase.

Can anybody help?

+3
source share
3 answers

Add this to the result:

SELECT 
    [TITLE] 
    case [TITLE] LIKE '%VALUE%' WHEN true then 1 else 0 end as CONTAINS_VALUE
    case [TITLE] LIKE '%SQL%' WHEN true then 1 else 0 end as CONTAINS_SQL
FROM 
    [M_TIPS] 
WHERE 
    [TITLE] LIKE '%VALUE%' OR 
    [TITLE] LIKE '%SQL%'; 
+3
source

A short in the WHERE * clause can interfere with any match count greater than 1 if you use OR.

How about saving the hit list in a table variable and then counting the hits,

DECLARE @MATCH TABLE (SCORE INT, TOKEN VARCHAR(16))
INSERT @MATCH 
      SELECT 1, 'VALUE'
UNION SELECT 1, 'SQL'
UNION SELECT 1, 'CAKE'

SELECT 
    [TITLE], SUM(M.SCORE)
FROM 
    [M_TIPS] T INNER JOIN @MATCH M ON T.[TITLE] LIKE '%' + M.TOKEN + '%'
GROUP BY T.[TITLE]

==
sql             1
value           1
xx sql value xx 2
+1
source

, , , :

SELECT [TITLE], SUM([CONDITION_COUNT]) FROM
(SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionString1 UNION ALL
SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionString2 UNION ALL
...
SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionStringN) SQ
GROUP BY [TITLE]
0

Source: https://habr.com/ru/post/1756741/


All Articles