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

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

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

    1. MySQL UPDATE 与来自不同表的 SELECT SUM

      时间:2023-10-09

        • <small id='KjMbR'></small><noframes id='KjMbR'>

            <tbody id='KjMbR'></tbody>

            • <i id='KjMbR'><tr id='KjMbR'><dt id='KjMbR'><q id='KjMbR'><span id='KjMbR'><b id='KjMbR'><form id='KjMbR'><ins id='KjMbR'></ins><ul id='KjMbR'></ul><sub id='KjMbR'></sub></form><legend id='KjMbR'></legend><bdo id='KjMbR'><pre id='KjMbR'><center id='KjMbR'></center></pre></bdo></b><th id='KjMbR'></th></span></q></dt></tr></i><div id='KjMbR'><tfoot id='KjMbR'></tfoot><dl id='KjMbR'><fieldset id='KjMbR'></fieldset></dl></div>
            • <tfoot id='KjMbR'></tfoot>
              <legend id='KjMbR'><style id='KjMbR'><dir id='KjMbR'><q id='KjMbR'></q></dir></style></legend>
              • <bdo id='KjMbR'></bdo><ul id='KjMbR'></ul>
              • 本文介绍了MySQL UPDATE 与来自不同表的 SELECT SUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有两张桌子:ITEMS 带有数量和 unit_price (id | name | order_id | qt | unit_price)和表 ORDERS.

                I have two tables: ITEMS with quantities and unit_price (id | name | order_id | qt | unit_price) and table ORDERS.

                我想UPDATEorders 并放入orders.total_price sum of multiplication of qt*unit_price 以获取相同订单的总价的订单.

                I want to UPDATE table orders and place in orders.total_price sum of multiplications qt*unit_price for the same orders to get total price of the order.

                对 items 表的 SELECT 查询非常简单,并且可以很好地为同一 order_id 中的所有项目提供总和:

                The SELECT query on the items table is quite simple and works fine giving sums for all items within the same order_id:

                SELECT SUM(items.qt*items.unit_price) from items GROUP by items.order_id
                

                但是我不能在我的 ORDERS 表中插入这个值.我无法完成这项工作:

                but I can't insert this value in my ORDERS table. I couldn't make this work:

                UPDATE orders, items SET orders.total_price = (SELECT SUM(items.qt*items.unit_price)
                FROM items GROUP BY items.order_id) WHERE orders.id = items.order_id
                

                它返回子查询返回多于1行"

                我发现了一个非常相似的问题 此处 但答案对我也不起作用:

                I found a very similar question here but the answer didn't work for me as well:

                UPDATE orders SET orders.t_price = (SELECT SUM(items.qt*items.unit_price) from items WHERE orders.id = items.order_id)
                

                推荐答案

                你可以UPDATE使用JOIN两个表:

                UPDATE Orders o 
                INNER JOIN
                (
                   SELECT order_id, SUM(qt * unit_price) 'sumu'
                   FROM items 
                   GROUP BY order_id
                ) i ON o.id = i.order_id
                SET o.total_price = i.sumu
                [WHERE predicate]
                

                这篇关于MySQL UPDATE 与来自不同表的 SELECT SUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:当表列相同时,EXCEPT 的执行速度是否比 JOIN 快 下一篇:使用 LEFT OUTER JOIN 检查相关行不存在的最佳方法是什么

                相关文章

                  • <bdo id='M0SIb'></bdo><ul id='M0SIb'></ul>
                  <legend id='M0SIb'><style id='M0SIb'><dir id='M0SIb'><q id='M0SIb'></q></dir></style></legend>
                  <i id='M0SIb'><tr id='M0SIb'><dt id='M0SIb'><q id='M0SIb'><span id='M0SIb'><b id='M0SIb'><form id='M0SIb'><ins id='M0SIb'></ins><ul id='M0SIb'></ul><sub id='M0SIb'></sub></form><legend id='M0SIb'></legend><bdo id='M0SIb'><pre id='M0SIb'><center id='M0SIb'></center></pre></bdo></b><th id='M0SIb'></th></span></q></dt></tr></i><div id='M0SIb'><tfoot id='M0SIb'></tfoot><dl id='M0SIb'><fieldset id='M0SIb'></fieldset></dl></div>
                1. <small id='M0SIb'></small><noframes id='M0SIb'>

                  <tfoot id='M0SIb'></tfoot>