简体   繁体   English

如何在BigQuery中查询名为“ OR”的列?

[英]How to query a column named “OR” in BigQuery?

We unfortunately have a table with a column that has been written with the field name of "OR". 不幸的是,我们有一个表,该表的列的字段名称为“ OR”。

If we try to query this table - "SELECT OR FROM etc." 如果我们尝试查询该表-“ SELECT OR FROM等”。 we get an error because OR is a reserved word. 我们收到一个错误,因为OR是保留字。

How do we query that column using BigQuery's legacy SQL queries? 我们如何使用BigQuery的旧版SQL查询来查询该列? (We need that column and not others.) (我们需要该列,而不需要其他列。)

We thought we could use "SELECT *" and BigQuery's "exclude" feature but isn't part of legacy SQL so we are unable to use it. 我们以为我们可以使用“ SELECT *”和BigQuery的“排除”功能,但它不是旧版SQL的一部分,因此我们无法使用它。 Other ideas? 还有其他想法吗?

I ran into this issue when querying the Hacker News dataset. 查询Hacker News数据集时遇到了这个问题。 It appears the authors added a "by" column because they were replicating the API response keys . 作者似乎添加了“ by”列, 因为他们正在复制API响应键 However, it's difficult to query and not best practice in database design . 但是,这很难查询, 也不是数据库设计中的最佳实践

I tried the brackets: 我尝试了括号:

SELECT
   [by] as author_name
FROM
   `bigquery-public-data.hacker_news.full`
LIMIT
   1000

but received the following error: 但收到以下错误:

Syntax error: Unexpected keyword BY at [2:4]

Here is a query that works on the Hacker News dataset in BigQuery with Standard SQL Dialect: 以下是适用于使用标准SQL方言的BigQuery中的Hacker News数据集的查询:

SELECT
  b.by as author_name
FROM
  `bigquery-public-data.hacker_news.full` as b
LIMIT
  1000

We found another SO answer for this - except you need to do something additional to what they describe. 我们为此找到了另一个SO答案 -除了您需要做的以外,他们还需要做其他一些事情。 You cannot just add brackets to the name - you also need to give it a new column name. 您不能只在名称中添加方括号-您还需要为其指定新的列名称。

The answer is to query like this: 答案是这样查询:

SELECT [OR] AS new_column_name FROM ...

I know that you're looking for an answer using legacy SQL (and you found one using the bracket syntax), but for future readers, or , and , etc. are valid column names for query results in standard SQL . 我知道您正在寻找使用旧版SQL的答案(并且您已经找到了使用方括号语法的答案),但对于将来的读者而言, orand等是标准SQL查询结果的有效列名。 The following query will not return an error, for instance: 例如,以下查询将不会返回错误:

WITH T AS (
  SELECT
    1 AS `or`,
    2 AS `and`
)
SELECT * FROM T;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM