SQL selects if number of WHERE certificates is satisfied

So, I have the following SQL statement with several WHERE clauses:

SELECT cols FROM table WHERE CONDITION1 OR CONDITION2 OR ... CONDITIONN 

I know that if I run this, then I will get all the lines that satisfy at least one of the above n conditions.

But now I want to return the rows so that at least k WHERE clauses are satisfied. Is there a way to do this in SQL without writing out all n Select k subsets of the WHERE clause set?

+5
source share
2 answers

This is pretty tiring, but it should work:

 SELECT cols FROM table WHERE CASE WHEN CONDITION1 THEN 1 ELSE 0 END + CASE WHEN CONDITION2 THEN 1 ELSE 0 END + CASE WHEN CONDITION3 THEN 1 ELSE 0 END + ... >= N ; 
+3
source
 SELECT cols FROM table WHERE CASE status CASE WHEN CONDITION1 THEN 1 CASE WHEN CONDITION2 THEN 1 END 

http://a2znotes.blogspot.in/2012/12/control-flow-functions.html is a very good resource for MySQL, if else, case statement. All are explained by examples.

0
source

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


All Articles