0

完全なレポートを作成するには、7 つの異なるテーブルから 8 つのクエリを 1 つのクエリに組み合わせて複雑なクエリを作成する必要があります。基本的な組み合わせはできますが、このクエリではうまくいきません。複雑な結合クエリについて Web を閲覧しましたが、うまくいきません...

すべてのテーブルは「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`

どんな助けでも大歓迎です...

4

1 に答える 1

3

OK、これを見てみましょう: 最適化の余地はたくさんありますが、単純にクエリを組み合わせることから始めます。

クエリ 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 の提案に従って、INNER 結合の代わりに LEFT を使用

編集:コメントのリクエスト後にIFNULL()を追加

于 2012-08-10T12:34:52.970 に答える