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

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

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

        带有左连接和分组依据的 MySQL 更新查询

        MySQL Update query with left join and group by(带有左连接和分组依据的 MySQL 更新查询)
      1. <tfoot id='7esPn'></tfoot>
      2. <small id='7esPn'></small><noframes id='7esPn'>

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

              <bdo id='7esPn'></bdo><ul id='7esPn'></ul>

                • <legend id='7esPn'><style id='7esPn'><dir id='7esPn'><q id='7esPn'></q></dir></style></legend>
                  本文介绍了带有左连接和分组依据的 MySQL 更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建更新查询,但在获取正确语法方面进展甚微.以下查询有效:

                  I am trying to create an update query and making little progress in getting the right syntax. The following query is working:

                  SELECT t.Index1, t.Index2, COUNT( m.EventType ) 
                      FROM Table t
                      LEFT JOIN MEvents m ON
                          (m.Index1 = t.Index1 AND
                           m.Index2 = t.Index2 AND
                          (m.EventType =  'A' OR m.EventType =  'B')
                      ) 
                      WHERE (t.SpecialEventCount IS NULL)
                      GROUP BY t.Index1, t.Index2
                  

                  它创建了一个三元组 Index1、Index2、EventCounts 的列表.它仅在 t.SpecialEventCount 为 NULL 的情况下执行此操作.我尝试编写的更新查询应该将此 SpecialEventCount 设置为该计数,即上面查询中的 COUNT(m.EventType).这个数字可以是 0 或任何正数(因此是左连接).Index1 和 Index2 在 Table t 中是唯一的,用于标识 MEvent 中的事件.

                  It creates a list of triplets Index1,Index2,EventCounts. It only does this for case where t.SpecialEventCount is NULL. The update query I am trying to write should set this SpecialEventCount to that count, i.e. COUNT(m.EventType) in the query above. This number could be 0 or any positive number (hence the left join). Index1 and Index2 together are unique in Table t and they are used to identify events in MEvent.

                  如何将选择查询修改为更新查询?IE.类似的东西

                  How do I have to modify the select query to become an update query? I.e. something like

                  UPDATE Table SET SpecialEventCount=COUNT(m.EventType).....
                  

                  但我很困惑应该把什么放在哪里,并因无数不同的猜测而失败.

                  but I am confused what to put where and have failed with numerous different guesses.

                  推荐答案

                  我认为 (Index1, Index2)Table 上的唯一键,否则我会预计对 t.SpecialEventCount 的引用会导致错误.

                  I take it that (Index1, Index2) is a unique key on Table, otherwise I would expect the reference to t.SpecialEventCount to result in an error.

                  编辑查询以使用子查询,因为它无法使用 GROUP BY

                  Edited query to use subquery as it didn't work using GROUP BY

                  UPDATE
                      Table AS t
                      LEFT JOIN (
                          SELECT
                              Index1,
                              Index2,
                              COUNT(EventType) AS NumEvents
                          FROM
                              MEvents
                          WHERE
                              EventType = 'A' OR EventType = 'B'
                          GROUP BY
                              Index1,
                              Index2
                      ) AS m ON
                          m.Index1 = t.Index1 AND
                          m.Index2 = t.Index2
                  SET
                      t.SpecialEventCount = m.NumEvents
                  WHERE
                      t.SpecialEventCount IS NULL
                  

                  这篇关于带有左连接和分组依据的 MySQL 更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)
                    <tbody id='v8Df2'></tbody>

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

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

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