{"id":139457,"date":"2020-07-14T14:57:59","date_gmt":"2020-07-14T06:57:59","guid":{"rendered":"http:\/\/4563.org\/?p=139457"},"modified":"2020-07-14T14:57:59","modified_gmt":"2020-07-14T06:57:59","slug":"%e6%b1%82%e9%97%ae%e5%90%84%e4%bd%8d-v-%e5%8f%8b-pg-%e5%ba%93%e7%9a%84%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=139457","title":{"rendered":"\u6c42\u95ee\u5404\u4f4d V \u53cb PG \u5e93\u7684\u6279\u91cf\u63d2\u5165\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u95ee\u5404\u4f4d V \u53cb PG \u5e93\u7684\u6279\u91cf\u63d2\u5165\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yuan434356430 <\/span>  <span><i><\/i> 22<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>oracle \u8fc1\u79fb\u5230 Pg \u8fc7\u7a0b\u4e2d\uff0c\u6539\u9020 merge \u4e3a with \u9012\u5f52\u5f62\u5f0f\uff0c\u4f46\u4e0d\u6e05\u695a\u600e\u4e48\u6539\u6210\u4f20\u5165 list \u5f62\u5f0f\uff1b \u7b80\u5316\u7684 sql \u5982\u4e0b<\/p>\n<pre><code>with upsert as (update xxx set state = #{state}  where code = #{code} and number = #{number} returning *) insert into xxx select #{id}, #{state}, #{code}, #{number} where (select count(*) from upsert) = 0;  int merge(DTO dto);  \u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u5355\u6761\u6570\u636e\u7684 upsert\uff0c\u4f46\u662f\u5bf9\u4f20\u5165 List&lt;DTO&gt;\u5f62\u5f0f\u6beb\u65e0\u5934\u7eea\uff0c\u5728\u6b64\u6c42\u52a9\u5e7f\u5927 V \u53cb~ <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2635534\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yuan434356430 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5144\u5f1f\u4eec\u6211\u81ea\u5df1\u627e\u5230\u89e3\u51b3\u65b9\u6848\u4e86\uff0c\u5199\u6cd5\u662f\u8fd9\u6837\u7684~<\/p>\n<p>with upsert as (update xxx set state = case when code= &#8216;1&#8217; and number = &#8216;1&#8217; then &#8216;1&#8217;<br \/>when code= &#8216;2&#8217; and number = &#8216;2&#8217; then &#8216;2&#8217;<br \/>end returning code, number returning *)<br \/>insert into xxx select &#8216;1&#8217;, &#8216;1&#8217;, &#8216;1&#8217;, &#8216;1&#8217;<br \/>where (select count(*) from upsert where &#8216;1&#8217; = upsert.code and &#8216;1&#8217; = upsert.number) = 0<br \/>UNION ALL SELECT &#8216;2&#8217;, &#8216;2&#8217;, &#8216;2&#8217;, &#8216;2&#8217;<br \/>where (select count(*) from upsert where &#8216;2&#8217; = upsert.code and &#8216;2&#8217; = upsert.number) = 0;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2635536\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yuan434356430 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u8fc7\u8fd8\u662f\u6709\u70b9\u7f3a\u9677\uff0c\u5c31\u662f case when \u6d89\u53ca\u4e0d\u5230\u7684\u6761\u4ef6\u4f1a\u7f6e\u7a7a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2635538\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yuan434356430 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u52a0\u4e86 where \u9650\u5b9a\u6761\u4ef6\uff0c\u5b8c\u7f8e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2635540\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yjhatfdu2 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             pg \u652f\u6301\u539f\u751f\u7684 upsert\uff0cinsert into tbl values(xx,xx) on conflict(some_unique_key) do update set xxx=exclude.xxx;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2635542\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yuan434356430 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yjhatfdu2 \u8868\u5206\u533a\u4e4b\u540e\u4e0d\u652f\u6301 on conflict\uff0c\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2635544\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yuan434356430 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9886\u5bfc\u6559\u6211\u4e00\u79cd\u65b9\u5f0f\u76f4\u63a5\u7528\u6700\u6734\u7d20\u7684\u65b9\u5f0f\uff0cforeach \u62fc sql\uff0c\u7136\u540e\u7528 pipeline \u63d0\u4ea4\u5230\u6570\u636e\u5e93                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u95ee\u5404\u4f4d V \u53cb PG \u5e93\u7684\u6279\u91cf\u63d2&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/139457"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=139457"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/139457\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=139457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=139457"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=139457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}