谷歌大查询不区分大小写的匹配

19

我该如何运行以下查询,就像我在mysql中做的一样:

SELECT * FROM [integrated-myth-15.testing_data_100k]
WHERE title='down in la'
现在,如果我有一个完美的区分大小写的字符串匹配,它将匹配,但是如果要对“down in la”进行不区分大小写的匹配,该怎么做呢?我是从Web控制台工作的。
3个回答

39

标准的做法是在输入字符串上使用LOWERUPPER函数,例如:

#legacySQL
SELECT * FROM [integrated-myth-15.testing_data_100k]
WHERE LOWER(title) = 'down in la';

或者:

#standardSQL
SELECT * FROM `integrated-myth-15.testing_data_100k`
WHERE LOWER(title) = 'down in la';

谢谢 - 出于好奇,为什么它默认为区分大小写的匹配,而不是普通的SQL不区分大小写除非另有规定的匹配? - David542
1
MySQL虽然被广泛使用,但并不很好地代表了SQL标准。例如,PostgreSQL也不会默认进行大小写不敏感的匹配。 - Elliott Brossard

4
最好的方法是在您的查询末尾添加“IGNORE CASE”。
SELECT * FROM [integrated-myth-15.testing_data_100k] WHERE title='down in la' IGNORE CASE

注意:这仅适用于旧版SQL。

正如官方文档中所提到的:

字符串函数用于操作字符串数据。字符串常量必须用单引号或双引号括起来。字符串函数默认区分大小写。您可以在查询的末尾添加IGNORE CASE以启用不区分大小写的匹配。IGNORE CASE仅适用于ASCII字符,并且仅在查询的顶层起作用。


1

如果我说错了,请原谅。我没有使用过该产品,我正在阅读文档以研究它。

我发现以下内容可能有用。

CONTAINS_SUBSTR

执行规范化、不区分大小写的搜索,以查看在表达式中是否存在一个值作为子字符串。如果该值存在,则返回 TRUE,否则返回 FALSE。

https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#contains_substr

这很有趣,因为大小写敏感性似乎内置于函数中,这告诉我可能还有其他工作方式类似的函数,并且它将按照大多数人的预期工作 :)

COLLATE

另外,我想知道是否可以在查询时应用排序规则来帮助。

https://cloud.google.com/bigquery/docs/reference/standard-sql/collation-concepts#collate_define

-- Assume there is a table with this column declaration:
CREATE TABLE table_a
(
    col_a STRING COLLATE 'und:ci',
    col_b STRING COLLATE '',
    col_c STRING,
    col_d STRING COLLATE 'und:ci'
);

-- This runs. Column 'b' has a collation specification and the
-- column 'c' does not.
SELECT STARTS_WITH(col_b_expression, col_c_expression)
FROM table_a;

-- This runs. Column 'a' and 'd' have the same collation specification.
SELECT STARTS_WITH(col_a_expression, col_d_expression)
FROM table_a;

-- This runs. Even though column 'a' and 'b' have different
-- collation specifications, column 'b' is considered the default collation
-- because it's assigned to an empty collation specification.
SELECT STARTS_WITH(col_a_expression, col_b_expression)
FROM table_a;

-- This works. Even though column 'a' and 'b' have different
-- collation specifications, column 'b' is updated to use the same
-- collation specification as column 'a'.
SELECT STARTS_WITH(col_a_expression, COLLATE(col_b_expression, 'und:ci'))
FROM table_a;

-- This runs. Column 'c' does not have a collation specification, so it uses the
-- collation specification of column 'd'.
SELECT STARTS_WITH(col_c_expression, col_d_expression)
FROM table_a;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接