舉例如下:
數據表為DemoTable,字段有id, condition1,condition2,condition3,condition4,condition5
要求是查詢DemoTable中,condition1,condition2,condition3,condition4,condition5五個字段中符合任意兩個或兩個以上的條件的內容。
可使用case when來實現這個條件,需要嵌套子查詢語句
sql語句代碼示例如下:
復制代碼 代碼如下:
SELECT * FROM DemoTable
WHERE ((SELECT CASE 1 WHEN condition1滿足條件 THEN 1 ELSE 0 END FROM DemoTable )
+(SELECT CASE 1 WHEN condition2滿足條件 THEN 1 ELSE 0 END FROM DemoTable)
+(SELECT CASE 1 WHEN condition3滿足條件 THEN 1 ELSE 0 END FROM DemoTable)
+(SELECT CASE 1 WHEN condition4滿足條件 THEN 1 ELSE 0 END FROM DemoTable)
+(SELECT CASE 1 WHEN condition5滿足條件 THEN 1 ELSE 0 END FROM DemoTable))>=2