<bdo id='Rm0j2'></bdo><ul id='Rm0j2'></ul>

  1. <small id='Rm0j2'></small><noframes id='Rm0j2'>

    <tfoot id='Rm0j2'></tfoot>
  2. <legend id='Rm0j2'><style id='Rm0j2'><dir id='Rm0j2'><q id='Rm0j2'></q></dir></style></legend>

  3. <i id='Rm0j2'><tr id='Rm0j2'><dt id='Rm0j2'><q id='Rm0j2'><span id='Rm0j2'><b id='Rm0j2'><form id='Rm0j2'><ins id='Rm0j2'></ins><ul id='Rm0j2'></ul><sub id='Rm0j2'></sub></form><legend id='Rm0j2'></legend><bdo id='Rm0j2'><pre id='Rm0j2'><center id='Rm0j2'></center></pre></bdo></b><th id='Rm0j2'></th></span></q></dt></tr></i><div id='Rm0j2'><tfoot id='Rm0j2'></tfoot><dl id='Rm0j2'><fieldset id='Rm0j2'></fieldset></dl></div>

      具有可变 WHERE 子句的批量更新表

      Bulk UPDATE table with WHERE clause that is variable(具有可变 WHERE 子句的批量更新表)
      <tfoot id='YGgSz'></tfoot>
          <bdo id='YGgSz'></bdo><ul id='YGgSz'></ul>
          • <legend id='YGgSz'><style id='YGgSz'><dir id='YGgSz'><q id='YGgSz'></q></dir></style></legend>
              <tbody id='YGgSz'></tbody>
                <i id='YGgSz'><tr id='YGgSz'><dt id='YGgSz'><q id='YGgSz'><span id='YGgSz'><b id='YGgSz'><form id='YGgSz'><ins id='YGgSz'></ins><ul id='YGgSz'></ul><sub id='YGgSz'></sub></form><legend id='YGgSz'></legend><bdo id='YGgSz'><pre id='YGgSz'><center id='YGgSz'></center></pre></bdo></b><th id='YGgSz'></th></span></q></dt></tr></i><div id='YGgSz'><tfoot id='YGgSz'></tfoot><dl id='YGgSz'><fieldset id='YGgSz'></fieldset></dl></div>

                <small id='YGgSz'></small><noframes id='YGgSz'>

                本文介绍了具有可变 WHERE 子句的批量更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一堆值对 [(foo1, bar1), (foo2, bar2), ...] 我想做一堆更新设置'foo' 列到 'foo1' ,其中 'bar' 列是 'bar1'".

                I have a bunch of pairs of values [(foo1, bar1), (foo2, bar2), ...] and I want to do a bunch of updates of "set the 'foo' column to 'foo1' where the 'bar' column is 'bar1'".

                我在 Python 中使用 psycopg2 执行此操作.我可以用查询 UPDATE table SET foo = %s WHERE bar = %s 来执行 executemany,但这是很多小的更新,而且会花很长时间.

                I am doing this in Python with psycopg2. I could do executemany with the query UPDATE table SET foo = %s WHERE bar = %s, but that's a lot of little updates and would take mad long.

                我怎样才能轻松快速地做到这一点?也许有临时表?

                How can I do this easily and fast? Perhaps something with a temp table?

                Postgres 9.3 版.

                Postgres version 9.3.

                推荐答案

                UPDATE tbl t 
                SET    foo = v.foo
                FROM  (
                   VALUES ('foo1'::text, 'bar1'::text), ('foo2', 'bar2'), ...
                   ) v(foo, bar)
                WHERE t.bar = v.bar;
                

                仅在值表达式的第一行中需要显式类型转换.示例中的 text - 可以是任何东西.后续行中的字符串文字被强制转换为相同的类型.

                Explicit type casts are only required in the first row of the values expression. text in the example - could be anything. String literal in subsequent rows are coerced to the same types.

                根据您拥有键值对的形式,其他方法可能更方便.像:创建一个临时表,COPY 到它,然后像任何其他表一样使用 UPDATE 中的临时表.详情:

                Depending on the form you have the key-value pairs, other methods may be more convenient. Like: create a temporary table, COPY to it, then use the temp table in the UPDATE like any other table. Details:

                • 如何在 Postgres 中使用 CSV 文件中的值更新选定的行?

                或者你可以并行传递两个简单的数组和unnest(Postgres 9.3的语法):

                Or you can pass two simple arrays and unnest in parallel (syntax for Postgres 9.3):

                UPDATE tbl t 
                SET    foo = v.foo
                FROM  (
                   SELECT unnest('{foo1,foo2,...}'::text[]) AS foo
                        , unnest('{bar1,bar2,...}'::text[]) AS bar
                   ) v(foo, bar)
                WHERE t.bar = v.bar;
                

                Postgres 9.4 有更好的办法:

                Postgres 9.4 has a better way:

                • PostgreSQL 中有没有类似 zip() 函数的东西,它结合了两个数组?

                这篇关于具有可变 WHERE 子句的批量更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                <i id='HEJBa'><tr id='HEJBa'><dt id='HEJBa'><q id='HEJBa'><span id='HEJBa'><b id='HEJBa'><form id='HEJBa'><ins id='HEJBa'></ins><ul id='HEJBa'></ul><sub id='HEJBa'></sub></form><legend id='HEJBa'></legend><bdo id='HEJBa'><pre id='HEJBa'><center id='HEJBa'></center></pre></bdo></b><th id='HEJBa'></th></span></q></dt></tr></i><div id='HEJBa'><tfoot id='HEJBa'></tfoot><dl id='HEJBa'><fieldset id='HEJBa'></fieldset></dl></div>
              1. <tfoot id='HEJBa'></tfoot>
                • <legend id='HEJBa'><style id='HEJBa'><dir id='HEJBa'><q id='HEJBa'></q></dir></style></legend>

                      <tbody id='HEJBa'></tbody>

                          <bdo id='HEJBa'></bdo><ul id='HEJBa'></ul>

                          <small id='HEJBa'></small><noframes id='HEJBa'>