在SQL查询中搜索'$'字符

问题描述 投票:0回答:2

如何在SQL查询中搜索“ $”特殊字符?

我正在尝试使主机以$字符结尾。这是Apache Impala。

我尝试过:

SELECT DISTINCT(host) FROM dclogwindows WHERE host LIKE '%[\$]'

但是我得到:

Done. 0 results.

也尝试过:

SELECT DISTINCT(host) FROM dclogwindows WHERE host LIKE '%$'

但结果相同

在全局搜索中,我看到很多这样的主机:

SELECT host FROM dclogwindows

H3932924934$
M4234278281$
M4545656543$ ...

无论我做什么,如果使用美元符号,结果都是0。

尝试过'%$%',尝试过喜欢,尝试过'%$'

资源管理器版本:Hue™3.11-IU de Hadoop

sql impala
2个回答
2
投票

您不需要逃避任何事情。以下应该工作:

WHERE host LIKE '%$'

'$'LIKE模式中不是特殊字符。在大多数数据库中,只有'%''_'是。有时,'\''['']''*'取决于数据库。

编辑:

您的问题可能在接口级别。我不确定如何在此处转义价值。也许:

WHERE host LIKE '%\$'

0
投票
  1. 正如其他人提到的那样,Apache Impala中的LIKE与ANSI SQL兼容,因此,如果您的列值LIKE '%$'结尾]。那么简单的$应该可以工作。
  2. [xxxxxxxx:21000] > select * from (select 'abc$' as col1 union select 'def') T where T.col1 like '%$';
    Query: select * from (select 'abc$' as col1 union select 'def') T where T.col1 like '%$'
    Query submitted at: 2019-10-14 12:00:00 (Coordinator: https://xxxxxxxx:25000)
    Query progress can be monitored at: https://xxxxxxxx:25000/query_plan?query_id=1742d1d149712d62:72715f7e00000000
    +------+
    | col1 |
    +------+
    | abc$ |
    +------+
    Fetched 1 row(s) in 0.12s
    
  1. 但是,如果值是逗号分隔的列表(如您在编辑中提供的内容,H3932924934$, M4234278281$, M4545656543$ ...),不以$结尾,则可以使用Impala的内置regexp_like函数过滤。
  2. [xxxxxxxx:21000] > select * from (select 'H3932924934$, M4234278281$, M4545656543$ ...' as col1 union select 'def') T where regexp_like (t.col1, '.*\\$');
    Query: select * from (select 'H3932924934$, M4234278281$, M4545656543$ ...' as col1 union select 'def') T where regexp_like (t.col1, '.*\\$')
    Query submitted at: 2019-10-14 12:00:00 (Coordinator: https://xxxxxxxx:25000)
    Query progress can be monitored at: https://xxxxxxxx:25000/query_plan?query_id=741c1c85c6b3f7e:ad00eeba00000000
    +----------------------------------------------+
    | col1                                         |
    +----------------------------------------------+
    | H3932924934$, M4234278281$, M4545656543$ ... |
    +----------------------------------------------+
    Fetched 1 row(s) in 0.02s
    [xxxxxxxx:21000] >
    

...或首先简单地尝试LIKE '%$%'以查看结果是否满足您的需求。


0
投票
  1. 正如其他人提到的那样,Apache Impala中的LIKE与ANSI SQL兼容,因此,如果您的列值LIKE '%$'结尾]。那么简单的$应该可以工作。
  2. [xxxxxxxx:21000] > select * from (select 'abc$' as col1 union select 'def') T where T.col1 like '%$';
    Query: select * from (select 'abc$' as col1 union select 'def') T where T.col1 like '%$'
    Query submitted at: 2019-10-14 12:00:00 (Coordinator: https://xxxxxxxx:25000)
    Query progress can be monitored at: https://xxxxxxxx:25000/query_plan?query_id=1742d1d149712d62:72715f7e00000000
    +------+
    | col1 |
    +------+
    | abc$ |
    +------+
    Fetched 1 row(s) in 0.12s
    
  1. 但是,如果值是逗号分隔的列表(如您在编辑中提供的内容,H3932924934$, M4234278281$, M4545656543$ ...),不以$结尾,则可以使用Impala的内置regexp_like函数过滤。
  2. [xxxxxxxx:21000] > select * from (select 'H3932924934$, M4234278281$, M4545656543$ ...' as col1 union select 'def') T where regexp_like (t.col1, '.*\\$');
    Query: select * from (select 'H3932924934$, M4234278281$, M4545656543$ ...' as col1 union select 'def') T where regexp_like (t.col1, '.*\\$')
    Query submitted at: 2019-10-14 12:00:00 (Coordinator: https://xxxxxxxx:25000)
    Query progress can be monitored at: https://xxxxxxxx:25000/query_plan?query_id=741c1c85c6b3f7e:ad00eeba00000000
    +----------------------------------------------+
    | col1                                         |
    +----------------------------------------------+
    | H3932924934$, M4234278281$, M4545656543$ ... |
    +----------------------------------------------+
    Fetched 1 row(s) in 0.02s
    [xxxxxxxx:21000] >
    

0
投票

您是否尝试过使用LIKE '%$%'

© www.soinside.com 2019 - 2024. All rights reserved.