将7个表中的8个复杂查询组合成一个查询

时间:2012-08-10 12:14:19

标签: mysql sql

我需要通过将来自7个不同表的8个查询组合到一个查询中来创建复杂查询,以创建完整报告。我可以做基本的组合,但这个问题对我来说越来越好。我浏览过网络以查找复杂的连接查询,但我的回答并不合适......

所有表格都通过' plant_id'进行关联,但有些数据是最新的条目,当前月份的一些计算条目等等,下面的查询都完全靠自己工作,但是我必须将它们组合成以下内容:

[plant_id,plant_make_id,plant_model],[current_plant_hrs,plant_hours_total],[plant_error_report],[plant_service_next],[plant_status_ddl_id],[使用的柴油总量,消费量],[location_id]

以下细分+个别查询: -

1 plant [ plant_id, plant_make_id, plant_model ],       
SELECT `plant_id`,`plant_make_id`,`plant_model`
 FROM `plant`

2 plant_hrs [ current_plant_hrs, plant_hours_total  ],           
A
SELECT `plant_hrs_stop`
FROM `plant_hrs` s1
WHERE `plant_hrs_date`=(SELECT MAX(s2.`plant_hrs_date`)
FROM `plant_hrs` s2
WHERE s1.`plant_id` = s2.`plant_id`)
ORDER BY `plant_id`
B
SELECT (MAX(`plant_hrs_stop`)- MIN(`plant_hrs_start`) )total_hrs
FROM`plant_hrs` 
WHERE MONTH(`plant_hrs_date`)= MONTH( CURRENT_DATE ) 
GROUP BY`plant_id`

3 plant_error [ plant_error_report ],               
SELECT `plant_error_report`
FROM `plant_error` s1
WHERE `plant_error_date`=(SELECT MAX(s2.`plant_error_date`)
FROM `plant_error` s2
WHERE s1.`plant_id` = s2.`plant_id`)
ORDER BY `plant_id`

4 plant_service [ plant_service_next ],             
SELECT `plant_service_hrs` , `plant_service_next`
FROM `plant_service` s1
WHERE `plant_service_date`=(SELECT MAX(s2.`plant_service_date`)
FROM `plant_service` s2
WHERE s1.`plant_id` = s2.`plant_id`)
ORDER BY `plant_id`

5 plant_status [ plant_status_ddl_id ],              
SELECT `plant_status_ddl_id`
FROM `plant_status`
WHERE `plant_status_date` = ( CURRENT_DATE ) - 1
GROUP BY `plant_id

6 diesel [ total diesel used, consumption ],            // 'plant_id = diesel_vehicle_no' 
SELECT (SUM(`diesel_qty`) )total_d
FROM`diesel` 
WHERE MONTH(`diesel_date`)= MONTH( CURRENT_DATE ) 
GROUP BY `diesel_vehicle_no`

7 plant_location [ location_id ]                    
SELECT `location_id` FROM `plant_location`
ORDER BY `plant_id`

非常感谢任何帮助......

1 个答案:

答案 0 :(得分:3)

好的,让我们来看看:我们将首先简单地组合您的查询 - 即使有很多优化空间。

查询1和7确实没什么 - 所以我们将使用他们的基表 - 其余的我们采取'按原样'

SELECT
  `plant`.`plant_id`,
  IFNULL(`qry2A`.`plant_hrs_stop`,''),
  IFNULL(`qry2B`.`total_hrs`,''),
  IFNULL(`qry3`.`plant_error_report`,''),
  IFNULL(`qry4`.`plant_service_next`,''),
  IFNULL(`qry5`.`plant_status_ddl_id`,''),
  IFNULL(`qry6`.`total_d`,''),
  IFNULL(`plant_location`.`location_id`'')
FROM
  -- Query 1: Use table
  `plant`
  -- Query 2A: Use as-is, but with the plant_id field and without the ORDER BY
  LEFT JOIN (
    SELECT `plant_hrs_stop`, s1.`plant_id` AS `plant_id`
    FROM `plant_hrs` s1
    WHERE `plant_hrs_date`=(SELECT MAX(s2.`plant_hrs_date`)
    FROM `plant_hrs` s2
    WHERE s1.`plant_id` = s2.`plant_id`)
  ) AS `qry2A` ON `plant`.`plant_id`=`qry2A`.`plant_id`
  -- Query 2B: Use as-is, but with the plant_id field
  LEFT JOIN (
    SELECT (MAX(`plant_hrs_stop`)- MIN(`plant_hrs_start`) ) AS total_hrs, `plant_id`
    FROM`plant_hrs` 
    WHERE MONTH(`plant_hrs_date`)= MONTH( CURRENT_DATE ) 
    GROUP BY`plant_id`
  ) AS `qry2B` ON `plant`.`plant_id`=`qry2B`.`plant_id`
  -- Query 3: Use as-is, but with the plant_id field and without the ORDER BY
  LEFT JOIN (
    SELECT `plant_error_report`,s1.`plant_id` AS `plant_id`
    FROM `plant_error` s1
    WHERE `plant_error_date`=(SELECT MAX(s2.`plant_error_date`)
    FROM `plant_error` s2
    WHERE s1.`plant_id` = s2.`plant_id`)
  ) AS `qry3` ON `plant`.`plant_id`=`qry3`.`plant_id`
  -- Query 4: Use as-is, but with the plant_id field and without the ORDER BY
  LEFT JOIN (
    SELECT `plant_service_hrs` , `plant_service_next`,s1.`plant_id` AS `plant_id`
    FROM `plant_service` s1
    WHERE `plant_service_date`=(SELECT MAX(s2.`plant_service_date`)
    FROM `plant_service` s2
    WHERE s1.`plant_id` = s2.`plant_id`)
  ) AS `qry4` ON `plant`.`plant_id`=`qry4`.`plant_id`
  -- Query 5: Use as-is, but with the plant_id field
  LEFT JOIN (
    SELECT `plant_status_ddl_id`,`plant_id`
    FROM `plant_status`
    WHERE `plant_status_date` = ( CURRENT_DATE ) - 1
    GROUP BY `plant_id
  ) AS `qry5` ON `plant`.`plant_id`=`qry5`.`plant_id`
  -- Query 6: Use as-is, but with the plant_id field
  LEFT JOIN (
    SELECT (SUM(`diesel_qty`) ) AS total_d, `diesel_vehicle_no` AS `plant_id`
    FROM`diesel` 
    WHERE MONTH(`diesel_date`)= MONTH( CURRENT_DATE ) 
    GROUP BY `diesel_vehicle_no`
  ) AS `qry6` ON `plant`.`plant_id`=`qry6`.`plant_id`
  -- Query 7: Use table
  LEFT JOIN `plant_location` ON `plant`.`plant_id`=`plant_location`.`plant_id`
ORDER BY
  `plant`.`plant_id`

修改:按@LuisSiquot

的建议使用LEFT代替INNER联接

修改:在评论中的请求后添加了IFNULL()