簡體   English   中英

將7個表中的8個復雜查詢組合為一個查詢

[英]Combining 8 Complex Queries from 7 Tables into one Query

我需要通過將來自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和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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM