[英]SQL SELECT. How to know what conditions in OR matched
我想知道 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';
我想得到這樣的結果:
結果 | |
---|---|
匹配的 | field1 匹配 field3 匹配 |
匹配的 | 字段 2 匹配 |
匹配的 | field1 匹配 field2 匹配 field3 匹配 |
每個條件列都需要一個單獨的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.