简体   繁体   English

SQL SELECT.如何知道OR匹配的条件

[英]SQL SELECT. How to know what conditions in OR matched

I want to know which of the conditions in ORs matched, making only one request The SQL below is wrong.我想知道 OR 中的哪些条件匹配,只做一个请求 下面的 SQL 是错误的。 What is a right way to do it?什么是正确的做法?

select 'MATCHED', 
case 
      WHEN flield1='xxx' THEN result=concat(result, ' field1 matched ' )
      WHEN flield2='yyy' THEN result=concat(result, ' field2 matched ' )
      WHEN flield3='zzz' THEN result=concat(result, ' field3 matched ' )
end as result 
from table 
where flield1='xxx' OR field2='yyyy' OR field3='zzzz';

I want to get result like this:我想得到这样的结果:

result结果
MATCHED匹配的 field1 matched field3 matched field1 匹配 field3 匹配
MATCHED匹配的 field2 matched字段 2 匹配
MATCHED匹配的 field1 matched field2 matched field3 matched field1 匹配 field2 匹配 field3 匹配

You need a separate case expression for each condition column:每个条件列都需要一个单独的case表达式:

select 'MATCHED',
       concat(
         case when flield1 = 'xxx'  then 'field1 matched ' else '' end,
         case when flield2 = 'yyyy' then 'field2 matched ' else '' end,
         case when flield3 = 'zzzz' then 'field3 matched'  else '' end) as result
from table 
where flield1 = 'xxx' OR field2 = 'yyyy' OR field3 = 'zzzz';

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

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