1. <tfoot id='L9y3w'></tfoot>

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

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

      1. <legend id='L9y3w'><style id='L9y3w'><dir id='L9y3w'><q id='L9y3w'></q></dir></style></legend>

        在 MYSQL 中加入带有 SUM 问题的表

        时间:2023-08-17
          <legend id='qeO13'><style id='qeO13'><dir id='qeO13'><q id='qeO13'></q></dir></style></legend>
          <tfoot id='qeO13'></tfoot>
              • <bdo id='qeO13'></bdo><ul id='qeO13'></ul>
              • <small id='qeO13'></small><noframes id='qeO13'>

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

                    <tbody id='qeO13'></tbody>
                  本文介绍了在 MYSQL 中加入带有 SUM 问题的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我一直在获取连接表上的 SUM 时遇到问题,总是有问题,我可以通过运行两个查询来获得我需要的结果,我想知道这两个查询是否可以组合成一个连接查询,这里是我的查询以及我加入查询的尝试

                  I have always had trouble getting SUMs on join tables, there is always an issue, I can get the results I need by running two queries, I am wondering if this two queries can be combine to make one join query, here is the queries I have and my attempt to join the query

                  查询 1

                  SELECT last_name, first_name, DATE_FORMAT( (mil_date),  '%m/%d/%y' ) AS dates, 
                  SUM( drive_time ) MINUTES FROM bhds_mileage LEFT JOIN bhds_teachers i 
                  ON i.ds_id = bhds_mileage.ds_id 
                  WHERE mil_date BETWEEN  '2016-04-11' AND  '2016-04-30'
                  AND bhds_mileage.ds_id =5
                  GROUP BY CONCAT( YEAR( mil_date ) ,  '/', WEEK( mil_date ) ) ,    
                  bhds_mileage.ds_id
                  ORDER BY last_name ASC , dates ASC 
                  

                  以分钟为单位的输出是271,281,279

                  the output in minutes is 271, 281, 279

                  查询 2

                  SELECT last_name, first_name, DATE_FORMAT((tm_date), '%m/%d/%y') AS dates,   
                  SUM(tm_hours) total FROM bhds_timecard LEFT JOIN bhds_teachers i 
                  ON i.ds_id = bhds_timecard.ds_id 
                  WHERE tm_date BETWEEN '2016-04-11' AND '2016-04-30' AND bhds_timecard.ds_id = 5
                  GROUP BY CONCAT(YEAR(tm_date), '/', WEEK(tm_date)), bhds_timecard.ds_id 
                  ORDER BY last_name ASC, dates ASC
                  

                  这里的输出是 33.00, 36.00, 26.75

                  The output here is 33.00, 36.00, 26.75

                  现在我尝试加入查询

                  SELECT last_name, first_name, DATE_FORMAT((tm_date), '%m/%d/%y') AS dates,  
                  SUM(tm_hours) total,  SUM( drive_time ) MINUTES FROM bhds_timecard 
                  LEFT JOIN bhds_teachers i ON i.ds_id = bhds_timecard.ds_id 
                  LEFT JOIN bhds_mileage ON DATE_FORMAT((bhds_timecard.tm_date), '%m/%d/%y') = 
                  DATE_FORMAT((bhds_mileage.mil_date), '%m/%d/%y') AND bhds_timecard.ds_id = bhds_mileage.ds_id
                  WHERE tm_date BETWEEN '2016-04-11' AND '2016-04-30' AND bhds_timecard.ds_id = 5
                  GROUP BY CONCAT(YEAR(tm_date), '/', WEEK(tm_date)), bhds_timecard.ds_id 
                  

                  括号是预期的

                  输出 1044 (271), 1086 (281), 1215 (279)

                  this outputs 1044 (271), 1086 (281), 1215 (279)

                  推荐答案

                  当您在主查询中使用多个连接时,您最终会得到所有表的叉积,因此总和乘以匹配的行数在另一张桌子上.您需要将总和移动到子查询中.

                  When you use multiple joins in the main query, you end up with a cross product of all the tables, so the sums get multiplied by the number of rows matching in another table. You need to move the sums into subqueries.

                  SELECT last_name, first_name, DATE_FORMAT(LEAST(mil_date, tm_date),  '%m/%d/%y' ) AS dates, 
                          total, minutes
                  FROM bhds_teachers AS i
                  LEFT JOIN (
                      SELECT ds_id, YEARWEEK(mil_date) AS week, MIN(mil_date) AS mil_date, SUM(drive_time) AS minutes
                      FROM bhds_mileage
                      WHERE mil_date BETWEEN '2016-04-11' AND  '2016-04-30'
                      AND bhds_mileage.ds_id = 5
                      GROUP BY ds_id, week) AS m 
                  ON m.ds_id = i.ds_id
                  LEFT JOIN (
                      SELECT ds_id, YEARWEEK(tm_date) AS week, MIN(tm_date) AS tm_date, SUM(tm_hours) AS total
                      WHERE tm_date BETWEEN '2016-04-11' AND '2016-04-30' AND bhds_timecard.ds_id = 5
                      GROUP BY ds_id, week) AS t 
                  ON t.ds_id = i.ds_id AND t.week = m.week
                  

                  这篇关于在 MYSQL 中加入带有 SUM 问题的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:获取所有产品、类别和元数据的 SQL 查询 woocomm 下一篇:MySQL 8.0 - 客户端不支持服务器请求的认证协议;

                  相关文章

                  最新文章

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