mysql SQL SELECT,如何知道OR中的哪些条件匹配

laximzn5  于 2023-01-29  发布在  Mysql
关注(0)|答案(1)|浏览(170)

我想知道OR中的哪些条件匹配,只发出一个请求。下面的SQL是错误的,正确的方法是什么?

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';

我想得到这样的结果:
| | 结果|
| - ------|- ------|
| 匹配|字段1匹配字段3匹配|
| 匹配|字段2匹配|
| 匹配|字段1匹配字段2匹配字段3匹配|

u3r8eeie

u3r8eeie1#

每个条件列都需要一个单独的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';

相关问题