当字符串来自其他 table 时,Hive like 不起作用

Hive like does not work when the string is from other table

我在 hive 中遇到一个奇怪的问题

table a:

id, domain
1,  m.taobao.com
2,  m.tmall.com

tableb

domain
%taobao\.com%
%tmall\.com%

当我使用:

Select a.id, a.domain from a where a like '%taobao\.com%' or a.like '%tmall\.com%' 

效果不错

但是当我使用

select a.id, a.domain from a, b where a.domain like b.domain

我得到 return 空值。

b.domain'%taobao\.com%''%tmall\.com%'。在 SQL 查询中直接使用它们有什么不同?是什么导致第二次查询失败?

模板中没有 '\' 它工作正常:

select a.*
from
(--Use your table instead of this subquery
  select stack(2,
              1,  'm.taobao.com',
              2,  'm.tmall.com') as (id, domain)
)a
cross join 
(--Use your table instead of this subquery
 select stack(2,
              '%taobao.com%',
              '%tmall.com%'
              ) as domain
) b
where a.domain like b.domain;

结果:

OK
1       m.taobao.com
2       m.tmall.com

和常量模板一样。这不会产生行:

select a.*
from
(--Use your table instead of this subquery
  select stack(2,
              1,  'm.taobao.com',
              2,  'm.tmall.com') as (id, domain)
)a
where a.domain like '%taobao\.com%'

这很好用:

where a.domain like '%taobao.com%'

DocumentationLIKE 运算符只识别 _% 模板: "The _ character in B matches any character in A (similar to . in posix regular expressions) while the % character in B matches an arbitrary number of characters in A (similar to .* in posix regular expressions). For example, 'foobar' like 'foo' evaluates to FALSE whereas 'foobar' like 'foo_ _ _' evaluates to TRUE and so does 'foobar' like 'foo%'." 所以,你不需要转义 '.' 对于 LIKE 运算符。

您需要转义 '.' 以匹配点字符,因为它仅用于 RLIKE 运算符,因为它使用 Java 正则表达式。