MogDB/openGauss 中 merge 的语法解析

近期了解学习了 MogDB/openGauss 中 merge 的使用,merge 语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入。简单来说就是有则更新,无则插入,语句简洁,效率高。

下面展示 MogDB/openGauss 中 merge 的语法

openGauss=# \h merge

Command: MERGE

Description: insert, update, or delete rows of a table based upon source data

Syntax:

MERGE [/*+ plan_hint */] INTO table_name [ [ AS ] alias ]

USING { { table_name | view_name } | subquery } [ [ AS ] alias ]

ON ( condition )

[

WHEN MATCHED THEN

UPDATE SET { column_name = { expression | DEFAULT } |

( column_name [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]

[ WHERE condition ]

]

[

WHEN NOT MATCHED THEN

INSERT { DEFAULT VALUES |

[ ( column_name [, ...] ) ] VALUES ( { expression | DEFAULT } [, ...] ) [, ...] [ WHERE condition ] }

];

创建测试表

merge 有几种匹配条件可以交叉选择。 作用: 判断源表和目标表是否满足合并的条件 如果满足

用源表去更新目标表

用源表去删除目标表

什么也不干

如果不满足

用源表去插入目标表

什么也不干

创建出满足的表

create table a_merge (

id int not null,

name varchar not null,

year int

);

create table b_merge (

id int not null,

aid int not null,

name varchar not null,

year int,

city varchar

);

create table c_merge (

id int not null,

name varchar not null,

city varchar not null

);

测试一:匹配则修改,无则插入

--插入数据

insert into a_merge values(1,'liuwei',20);

insert into a_merge values(2,'zhangbin',21);

insert into a_merge values(3,'fuguo',20);

insert into b_merge values(1,2,'zhangbin',30,'吉林');

insert into b_merge values(2,4,'yihe',33,'黑龙江');

insert into b_merge (id,aid,name,city) values(3,3,'fuguo','山东');

--数据对比

select * from a_merge; select * from b_merge;

id | name | year

----+----------+------

1 | liuwei | 20

2 | zhangbin | 21

3 | fuguo | 20

(3 rows)

id | aid | name | year | city

----+-----+----------+------+--------

1 | 2 | zhangbin | 30 | 吉林

2 | 4 | yihe | 33 | 黑龙江

3 | 3 | fuguo | | 山东

(3 rows)

--merge语句

merge into a_merge a

using (select b.aid,b.name,b.year from b_merge b) c on (a.id=c.aid)

when matched then

update set year=c.year

when not matched then

insert values(c.aid,c.name,c.year);

--更新后的a_merge表

select * from a_merge;

id | name | year

----+----------+------

1 | liuwei | 20

2 | zhangbin | 30

3 | fuguo |

4 | yihe | 33

(4 rows)

测试二:匹配则修改,无则不操作

--插入数据

insert into b_merge values(4,1,'liuwei',80,'江西');

insert into b_merge values(5,5,'tiantian',23,'河南');

--核对数据

select * from a_merge;select * from b_merge;

id | name | year

----+----------+------

1 | liuwei | 20

2 | zhangbin | 30

3 | fuguo |

4 | yihe | 33

(4 rows)

id | aid | name | year | city

----+-----+----------+------+--------

1 | 2 | zhangbin | 30 | 吉林

2 | 4 | yihe | 33 | 黑龙江

3 | 3 | fuguo | | 山东

4 | 1 | liuwei | 80 | 江西

5 | 5 | tiantian | 23 | 河南

(5 rows)

--merge语句

merge into a_merge a

using (select b.aid,b.name,b.year from b_merge b) c on (a.id=c.aid)

when matched then

update set year=c.year;

--数据对比

select * from a_merge;

id | name | year

----+----------+------

1 | liuwei | 80

2 | zhangbin | 30

3 | fuguo |

4 | yihe | 33

(4 rows)

测试三:匹配无操作,不匹配进行 insert

--修改测试数据

update b_merge set year=70 where aid=2;

--两表对比

select * from a_merge;select * from b_merge;

id | name | year

----+----------+------

1 | liuwei | 80

2 | zhangbin | 30

3 | fuguo |

4 | yihe | 33

(4 rows)

id | aid | name | year | city

----+-----+----------+------+--------

2 | 4 | yihe | 33 | 黑龙江

3 | 3 | fuguo | | 山东

4 | 1 | liuwei | 80 | 江西

5 | 5 | tiantian | 23 | 河南

1 | 2 | zhangbin | 70 | 吉林

(5 rows)

--merge语句

merge into a_merge a

using (select b.aid,b.name,b.year from b_merge b) c on (a.id=c.aid)

when not matched then

insert values(c.aid,c.name,c.year);

--查看a_merge表

select * from a_merge;

id | name | year

----+----------+------

1 | liuwei | 80

2 | zhangbin | 30

3 | fuguo |

4 | yihe | 33

5 | tiantian | 23

(5 rows)

测试四:一律 insert

--merge语句

merge into c_merge c

using (select b.aid,b.name,b.city from b_merge b) b on (1=0)

when not matched then

insert values(b.aid,b.name,b.city);

--查看两表,条数相同

select * from c_merge ;select * from b_merge ;

id | name | city

----+----------+--------

3 | fuguo | 山东

5 | tiantian | 河南

2 | zhangbin | 吉林

4 | yihe++ | 黑龙江

1 | liuwei++ | 江西

6 | ningqin | 江西

7 | bing | 吉安

(7 rows)

id | aid | name | year | city

----+-----+----------+------+--------

3 | 3 | fuguo | | 山东

5 | 5 | tiantian | 23 | 河南

1 | 2 | zhangbin | 70 | 吉林

2 | 4 | yihe++ | 33 | 黑龙江

4 | 1 | liuwei++ | 80 | 江西

6 | 6 | ningqin | 23 | 江西

7 | 7 | bing | 24 | 吉安

(7 rows)

MogDB/openGauss中merge的语法解析的更多相关文章

  1. Oracle中merge into语法

    merge into 语句就是insert和update的一个封装,简单来说就是: 有则更新,无则插入 下面说怎么使用 MERGE INTO table_Name  T1(匿名) using (另外一 ...

  2. oracle中merge into用法解析

    merge into的形式: MERGE INTO [target-table] A USING [source-table sql] B ON([conditional expression] an ...

  3. 在.NET Core中使用Irony实现自己的查询语言语法解析器

    在之前<在ASP.NET Core中使用Apworks快速开发数据服务>一文的评论部分,.NET大神张善友为我提了个建议,可以使用Compile As a Service的Roslyn为语 ...

  4. python在lxml中使用XPath语法进行#数据解析

    在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...

  5. Java中的static关键字解析

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

  6. Oracle中Merge into用法总结

    MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,无法匹配的执 ...

  7. With语句以及@contextmanager的语法解析

    with 语句以及@contextmanager的语法解析   with语句可以通过很简单的方式来替try/finally语句. with语句中EXPR部分必须是一个包含__enter__()和__e ...

  8. MySQL- -Join语法解析与性能分析

    Mysql Join语法解析与性能分析 一.Join语法概述 join 用于多表中字段之间的联系,语法如下: ... FROM table1 INNER|LEFT|RIGHT JOIN table2 ...

  9. Java中的static关键字解析 转载

    原文链接:http://www.cnblogs.com/dolphin0520/p/3799052.html Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到 ...

  10. oracle中merge的详解

    Oracle在9i引入了merge命令, 通过这个merge你能够在一个SQL语句中对一个表同时执行inserts和updates操作. 当然是update还是insert是依据于你的指定的条件判断的 ...

随机推荐

  1. 【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable

    问题描述 在VS Code中编写好 Azure Function App代码后,通过  func azure functionapp publish 部署失败,抛出 503 Service Unava ...

  2. RocketMQ(2) 消息的生产和存储

    ## 一 : 消息的生产 1. 消息的生产过程 Producer在发送消息时可以将消息写入到指定topic的某Broker中的某Queue中,其经历了如下过程: Producer发送消息之前,会先向N ...

  3. Java对象引用和内存管理的细节

    在Java中,当局部变量(比如方法参数)的作用域结束时,这个局部变量的引用确实不再存在,但这并不意味着它引用的对象会被销毁.对象的销毁是由Java的垃圾回收器(Garbage Collector, G ...

  4. centos7 开机自动执行脚本

    1.因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod +x /etc/rc.d/rc.local 2.赋予脚本可执行权限假设/usr/loc ...

  5. 软件开发、持续集成(CI)、持续交付(CD)、持续部署(CD) 和 版本管理(Version Control) 的理解和思考

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  6. Ubuntu 14.04傻瓜式安装 0@0

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  7. 01.Android崩溃Crash封装库

    目录介绍 01.该库具有的功能 02.该库优势分析 03.该库如何使用 04.降低非必要crash 05.异常恢复原理 06.后续的需求说明 07.异常栈轨迹原理 08.部分问题反馈 09.其他内容说 ...

  8. Android之Window和弹窗问题

    目录介绍 10.0.0.1 Window是什么?如何通过WindowManager添加Window(代码实现)?WindowManager的主要功能是什么? 10.0.0.2 Window概念解析?W ...

  9. 记录--7个Js async/await高级用法

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 7个Js async/await高级用法 JavaScript的异步编程已经从回调(Callback)演进到Promise,再到如今广泛使 ...

  10. Anaconda 创建新环境

    使用conda 命令创建一个名为 python311 的python版本为3.11的环境 conda create -n python311 python=3.11 接着使用 conda activa ...