简体   繁体   English

在编写针对bigQuery的查询时,如何使用变量而不是硬编码来写入表名和列名

[英]While writing query for bigQuery how to write table name and column name using variables instead of hard coding

In the below mentioned query projectname-dataset-tableName is hardcoded in the query 在下面提到的查询中,projectname-dataset-tableName在查询中进行了硬编码

query = """
SELECT city from bigquery-public-data.openaq.global_air_quality WHERE country = 'IN'
"""

How to write the same in more dynamic way? 如何以更动态的方式编写相同的内容? The fulltableid attribute is not returning compatible format. fulltableid属性未返回兼容格式。

query1 = """
SELECT city from """ + str(tableGAQ.full_table_id) + """ WHERE country = 'IN'
"""

If you had the following query: 如果您有以下查询:

query = (
"SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` "
'WHERE state = "TX" '
"LIMIT 100"
)

To use a variable to dynamically introduce your table ID, your query will look like this: 要使用变量动态引入您的表ID,查询将如下所示:

tableID = "`bigquery-public-data.usa_names.usa_1910_2013`"
query = (
"SELECT name FROM"+ tableID + "
'WHERE state = "TX" '
"LIMIT 100"

)

Notice that your table name has to be written between ` `. 注意,您的表名必须写在``之间。

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

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