如何插入非重复项,是否可以使用合并和自引用?

nr9pn0ug  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(353)

我需要插入不重复的值。。。此“与自引用合并”未按预期工作,但未生成错误消息。。。我能用类似的东西吗?

MERGE INTO mydb.mytab AS Tref 
USING mydb.mytab AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (123, 'etc');

(更新但不改变问题)这里所建议的其他结构似乎是

INTO INTO mydb.mytab 
  SELECT t1.* 
  FROM ( select stack(2, 123,'ect1',  20,'etc2') as (id,etc) ) t1
  LEFT OUTER JOIN mydb.mytab t2 ON t1.id = t2.id
  WHERE t1.id is null
;

它不优雅(还有其他?),但它是工作(!)。合并还有别的选择吗?

niknxzdl

niknxzdl1#

只有当mydb.mytab不包含匹配行时,merge才会插入

MERGE INTO mydb.mytab AS Tref 
USING  ( select stack(2, 123,'ect1',  20,'etc2') as (id,etc) ) AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (t.id, t.etc) --columns positions should match those in Tref 
;

相关问题