【问题标题】:Redshift LIKE column value with % in WHERE clauseWHERE 子句中带有 % 的 Redshift LIKE 列值
【发布时间】:2018-10-27 14:38:30
【问题描述】:

我正在努力让它发挥作用:

with a as (select '1'::varchar(32) as x), 
     b as (select '10'::varchar(32) as x)
select *
from b 
left join a using(x)
where b.x like '%1%'
;

x    
---- 
10   

选择了 1 条记录 [获取元数据:0ms] [获取数据:0ms]

但我需要使用的是:

where b.x like '%' || a.x || '%'
;

x    
---- 

0 record(s) selected [Fetch MetaData: 0ms] [Fetch Data: 0ms] 

有什么想法吗?

我已经把这个问题放在这里了Redshift LIKE column value with % 这个想法从何而来。

谢谢

【问题讨论】:

  • 请分享输入数据和预期输出。为什么要使用第二种语法?
  • 看看strpos函数看看a是否在b内
  • @Swapnil,预期输出在第一个块,x=10
  • 不要将问题编辑到invalidate existing answers。如果答案与当前答案不同,您可以随时发布答案。

标签: string amazon-redshift sql-like


【解决方案1】:

可以使用strpos函数查看a是否在b内

https://docs.aws.amazon.com/redshift/latest/dg/r_STRPOS.html

where strpos(b,a) > 0

【讨论】:

  • 当我以这种方式使用 strpos() 时 Select strpos('10', '1') >0 然后工作,换句话说,它是 'T​​RUE' 但它不在下面的代码中. with a as (select '1'::varchar(32) as x), b as (select '10'::varchar(32) as x) select * from b left join a using(x) where strpos(b.x, a.x) > 0 ;我错过了什么?
  • @gafeta 在您的示例中,a.x = b.x(由于using(x)),因此答案将始终为零。我建议您编辑您的问题以显示示例输入表,然后显示您想要的输出示例,然后我们可以更好地为您提供帮助。
  • @John Rotenstein 你是完全正确的。感谢您的意见,我想我现在没有一个,而是两个解决方案。
猜你喜欢
  • 2014-09-20
  • 2018-03-08
  • 2012-06-10
  • 1970-01-01
  • 2013-06-27
  • 2019-06-02
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
相关资源
最近更新 更多