Sql 如何在配置单元中的数组_contains函数中模式匹配字符串表达式?
将列a视为字符串数组。现在我想搜索表中所有具有特定模式字符串的行。我知道array_contains()函数可以用来检查精确匹配,但是有没有一种方法可以检查模式匹配Sql 如何在配置单元中的数组_contains函数中模式匹配字符串表达式?,sql,arrays,string,hive,Sql,Arrays,String,Hive,将列a视为字符串数组。现在我想搜索表中所有具有特定模式字符串的行。我知道array_contains()函数可以用来检查精确匹配,但是有没有一种方法可以检查模式匹配 select * from table where array_contains(A,"%123%"); 可以这样进行模式匹配吗?首先生成行号。然后用分解数组列,然后筛选出与模式不匹配的行,例如 with table1 as ( select row_number() over () as rnum,
select * from table
where array_contains(A,"%123%");
可以这样进行模式匹配吗?首先生成行号。然后用分解数组列,然后筛选出与模式不匹配的行,例如
with table1 as (
select row_number() over () as rnum, *
from your_table
),
table2 as (
select *, arr
from table1 lateral view explode(A) tmp as arr
where arr like '%123%'
),
table3 as (
select distinct rnum from table2
)
select *
from table1 t1
left semi
join table3 t3
on t1.rnum = t3.rnum
我认为你可以使用:
select distinct t1.*
from table1 t1 lateral view
explode(t1.A) tmp as arr
where arr like '%123%';
有一种方法可以消除选择distinct
,但我不确定Hive是否支持它:
select t1.*
from table1 t1
where exists (select 1
from explode(t1.A) as arr
where arr like '%123%'
);
或者您可以使用这种有点笨拙的方法:
select t1.*
from table1 t1
where concat_ws('|', t1.A) like '%123%';