如何在 Clickhouse 数据库中搜索不区分大小写的字符串?

How to search the string in query with case insensitive on Clickhouse database?

我正在使用 PHP 和后端 ClickHouse 数据库开发网站。当我使用类似查询时,它不支持区分大小写的单词。

select id,comments from discussion where  comments LIKE "%Data not reflect%";

有什么方法可以搜索不区分大小写的词吗?

没有 ILIKE 运算符。我想你可以使用 lowerUTF8().

select id,comments from discussion where lowerUTF8(comments) LIKE '%Data not reflect%';

但是,性能可能很重,因为它必须将所有 comments 值转换为小写。

使用positionCaseInsensitivepositionCaseInsensitiveUTF8

就这样

SELECT id,comments
FROM discussion
WHERE positionCaseInsensitive(comments,'Data not reflect')>0;

对于更复杂的模式,您可以使用带有 i 标志的正则表达式:

SELECT ... WHERE match(comment, '(?i)Data.*not reflect');

查看文档:https://clickhouse.yandex/docs/en/query_language/functions/string_search_functions/#position-haystack-needle-locate-haystack-needle

不区分大小写 ILIKE 运算符已添加到以 version 20.6.3.28:

开头的 CH
SELECT *
FROM
(
    SELECT '** Data not reflect **' AS text
    UNION ALL
    SELECT '** data not reflect **'
)
WHERE text ILIKE '%Data not reflect%'

/*
┌─text───────────────────┐
│ ** Data not reflect ** │
│ ** data not reflect ** │
└────────────────────────┘
*/