Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update with join with BigQuery

Is it possible to do an UPDATE on a table based on a JOIN with an existing table in BigQuery?

When I try this statement on the following database (https://bigquery.cloud.google.com/dataset/pfamdb:pfam31),

  UPDATE pfam31.uniprot
  SET uniprot.auto_architecture = uniprot_architecture.auto_architecture
  INNER JOIN
      pfam31.uniprot_architecture using(uniprot_acc)

I get errors relating to the INNER JOIN, with WHERE being expected instead. How should I be doing this (if it's possible)?

like image 986
saladi Avatar asked Nov 19 '17 04:11

saladi


People also ask

Can we use update with join?

The most easiest and common way is to use join clause in the update statement and use multiple tables in the update statement. Here we can see that using join clause in update statement. We have merged two tables by the use of join clause.

Can you update data in BigQuery?

The BigQuery data manipulation language (DML) enables you to update, insert, and delete data from your BigQuery tables. You can execute DML statements just as you would a SELECT statement, with the following conditions: You must use Google Standard SQL.

Can you do joins in BigQuery?

Google BigQuery does not support other join types, such as a full outer join or right outer join. In addition, Google BigQuery uses the default equals (=) operator to compare columns and does not support other operators.

How do you optimize joins in BigQuery?

Optimize your join patterns When you create a query by using a JOIN , consider the order in which you are merging the data. The Google Standard SQL query optimizer can determine which table should be on which side of the join, but it is still recommended to order your joined tables appropriately.


2 Answers

UPDATE `pfam31.uniprot` a
SET a.auto_architecture = b.auto_architecture
FROM `pfam31.uniprot_architecture` b
WHERE a.uniprot_acc = b.uniprot_acc
like image 99
Mikhail Berlyant Avatar answered Sep 18 '22 08:09

Mikhail Berlyant


Please refer to the UPDATE statement syntax. There is even an example on UPDATE with JOIN. You need to use a FROM clause, and your query should be something like this:

UPDATE pfam31.uniprot
SET uniprot.auto_architecture =
  (SELECT uniprot_architecture.auto_architecture
   FROM pfam31.uniprot_architecture
   WHERE uniprot.uniprot_acc = auto_architecture.uniprot_acc);

This assumes that there is a 1:1 relationship between the uniprot_acc values in the tables. If that isn't the case, you will need to use LIMIT 1, for instance.

like image 35
Elliott Brossard Avatar answered Sep 18 '22 08:09

Elliott Brossard