【问题标题】:Bigquery delete rows that are present in another tableBigquery 删除另一个表中存在的行
【发布时间】:2021-09-28 23:33:00
【问题描述】:

我想删除表 t1 中存在于表 t2 中的所有行。

table_1 is as follows

a   b   c
1   4   3
3   334 3
5   4   5
6   5   4
4   85  3
7   332 54
8   46  6
45  42  5
7   576 6

表2如下

a   b   c
7   332 54
3   334 3
7   576 6

如前所述,我想删除表 t1 中存在于表 t2 中的所有行。

所以我使用了代码

DELETE `projectname.datasetname.table1` t
WHERE t IN (SELECT * from `projectname.datasetname.table2`)

但它不起作用,这里的理想解决方案是什么? 我想要的结果是

a   b   c
1   4   3
5   4   5
6   5   4
4   85  3
8   46  6
45  42  5

谢谢

【问题讨论】:

  • “没有帮助”是什么意思?样本数据和期望的结果也会有所帮助。
  • Table_1 有 10 行,而 table_2 有 3 行,Table_2 中的相同 3 行在 Table_1 中可用。我想从 Table_1 中删除这 3 行,以便我现在有 Table_1 有 7 行:没有帮助意味着代码不起作用

标签: sql google-bigquery google-cloud-functions bigquery-udf


【解决方案1】:

如果需要查看整条记录,可以使用:

DELETE `projectname.datasetname.table1` t
WHERE EXISTS (SELECT 1
              FROM `projectname.datasetname.table2` t2
              WHERE TO_JSON_STRING(t2) = TO_JSON_STRING(t)
             );

但是,通常对某种简单的 id 列进行比较通常足以进行此类比较。

【讨论】:

    【解决方案2】:

    在下面使用

    DELETE `projectname.datasetname.table1` t
    WHERE TO_JSON_STRING(t) IN (SELECT TO_JSON_STRING(t) from `projectname.datasetname.table2` t);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2013-12-26
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多