简体   繁体   English

匹配值列sql

[英]match vlue column sql

Helllo, Im tying to match value of col1 in tab1.你好,我想匹配 tab1 中 col1 的值。 IF value == 'Tri/' then tab1.col2 == 's1'.如果值 == 'Tri/' 然后 tab1.col2 == 's1'。 IF value == 'Tri/v1/v11/' then 's2'. IF 值 == 'Tri/v1/v11/' 然后是 's2'。 I wrote the code below but not works in my case (if col1 == 'Tri/v1/v11/' then s1 which not what im seeking.我写了下面的代码,但在我的情况下不起作用(如果 col1 == 'Tri/v1/v11/' 那么 s1 这不是我想要的。

case
when vh_state like '%Tri/%' then 's1'
when vh_state like'%Tri/v1/v11/%' then 's2'
else 'UNkown choice'
end as col2

These are evaluated in order, so you need to put the most restrictive first:这些是按顺序评估的,所以你需要把最严格的放在第一位:

(case when vh_state like '%Tri/v1/v11/%' then 's2'
      when vh_state like '%Tri/%' then 's1'
      else 'Inconnu'
 end) as col2

If you don't readily know which is the most restrictive, you could use a subquery to match the longest:如果您不知道哪个是最严格的,您可以使用子查询来匹配最长的:

(select v.value
 from (values ('%Tri/v1/v11/%', 's2'),
              ('%Tri/%', 's1'),
      ) v(pattern, value)
 where vh_state like v.pattern
 order by length(pattern) desc  
 limit 1
)               

The issue is you have used '%Tri/%' in the first case statement, which qualifies the value Tri/v1/v11/ since it is a like operator resulting in S1 for both the cases.问题是您在第一个 case 语句中使用了'%Tri/%' ,它限定了值Tri/v1/v11/ ,因为它是一个类似的运算符,导致两种情况的 S1。 Change the order of case statement to make it work properly更改case语句的顺序,使其正常工作

Use below query,使用以下查询,

case when vh_state like '%Tri/v1/v11/%' then 's2'
  when vh_state like '%Tri/%' then 's1'
  else 'Inconnu'
 end as col2

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

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