简体   繁体   English

查询以从一个表中获取一个条件的总和,然后将其添加到另一张表中的相同条件中

[英]Query to get the sum of a condition from one table, and add it to same condition in another table

What I have is two tables; 我有两张桌子;

heli_acft : heli_acft

  • acft_id acft_id
  • acft_reg acft_reg
  • acft_hours acft_hours

heli_flt : heli_flt

  • flt_id flt_id
  • flt_acft flt_acft
  • flt_colhr flt_colhr

acft_id correlates to flt_acft . acft_id关联到flt_acft

I want to get the total sum of all flt_colhr of each flt_acft in the table, and then add that figure to the value of acft_hours per acft_id . 我想获取表中每个flt_acft的所有flt_colhr的总和,然后将该数字添加到每个acft_idacft_hours的值中。

The result will then be displayed as a <?php echo data[???] ?> in a table. 结果将在表中显示为<?php echo data[???] ?>

Any ideas on how I could do this? 关于如何执行此操作的任何想法?

Join the tables and calculate the sum. 合并表格并计算总和。 I use a LEFT JOIN to get a zero result for aircraft that have no flights. 对于没有飞行的飞机,我使用LEFT JOIN获得零结果。

SELECT acft_id, acft_hours + IFNULL(SUM(flt_colhr), 0) AS total_colhr
FROM heli_acft 
LEFT JOIN heli_flt ON acft_id = flt_acft
GROUP BY acft_id

Assuming that acft_id is a unique idenfifier in flt_acft table... 假设acft_idflt_acft表中的唯一标识符...

Here is one approach to returning the specified resultset, adding the total of "hours" (from heli_flt to the "hours" from heli_acft : 下面是一个方法来返回结果集指定,增加的总的“小时”(来自heli_flt从“小时” heli_acft

 SELECT a.acft_id
      , a.acft_hours
      , IFNULL(f.flt_hours,0)
      , a.acft_hours + IFNULL(f.flt_hours,0) AS total_hours
   FROM heli_acft a
   LEFT
   JOIN ( SELECT SUM(h.flt_colhr) AS flt_hours
               , h.flt_acft
            FROM heli_flt h
           GROUP BY h.flt_acft
        ) f
     ON f.flt_acft = a.acft_id

The inline view calculates f calculates the sum of the hours from heli_flt for each flt_acft . 内联视图计算f为每个flt_acftheli_flt计算小时的总和。 A JOIN to heli_acft to get the acft_hours , and we simply add the values of the two columns. JOIN到heli_acft以获得acft_hours ,我们只需将两列的值相加即可。 We use an "outer" join so we can return rows from heli_acft that don't have any related rows in heli_flt , and use an IFNULL() function to replace a NULL value with a zero. 我们使用“外部”加入,所以我们可以从返回行heli_acft没有任何相关行heli_flt ,并使用IFNULL()函数来替换零NULL值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于来自另一个表的条件的SUM列 - SUM columns based on condition from another table 从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录 - Retrieve data from a table with a condition where and check the record in another table in one SELECT query 如何在不同条件下对来自同一表的数据求和? - How to sum data from the same table with different condition? 从一个表中选择行,但语句条件在另一个表中 - Pick rows from one table but where statement condition is in another table 如何在另一个表中执行的mysql查询中添加条件? - how can i add condition in mysql query executed in another table? 如何通过相同条件值连接另一个表中的值 - how to join value from another table by same condition value 如何从表中选择具有相同查询但条件不同的数据库中的数据? - How to select data from database with same query but different condition in a table? 将数据插入一个表中,从另一个表中删除,并在MySQL中使用时间戳记条件 - Insert data in one table, remove from another, with timestamp condition in mysql 条件在2个表中,但仅从一个表中选择 - condition in 2 table but select just from one table 我正在尝试运行一个查询,该查询从一个表中获取值并将其用作获取值或对另一表执行操作的条件 - I am trying to run a query that takes value from one table and uses it as condition to fetch value or execute action on another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM