我需要通过将来自 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`
任何帮助将非常感激...