Mysql Выборка данных с сортировкой из трех таблиц

B1ackGh0st

Новичок
Решил проблему сам... частично. Остался вопрос как мне уменьшить количество запросов

$sql_cost_in_year = "SELECT SUM(oe.cost_in_year) FROM `materials` m
INNER JOIN cat c ON c.id = m.id_cat
INNER JOIN ordered_equipment oe ON m.id = oe.name
WHERE oe.year=".$year_report." AND oe.id_department=".$department_report." AND id_cat=".$array_report_generation['id_cat'];

$sql_cost_quarter_1 = "SELECT SUM(oe.cost_quarter_1) FROM `materials` m
INNER JOIN cat c ON c.id = m.id_cat
INNER JOIN ordered_equipment oe ON m.id = oe.name
WHERE oe.year=".$year_report." AND oe.id_department=".$department_report." AND id_cat=".$array_report_generation['id_cat'];

$sql_cost_quarter_2 = "SELECT SUM(oe.cost_quarter_2) FROM `materials` m
INNER JOIN cat c ON c.id = m.id_cat
INNER JOIN ordered_equipment oe ON m.id = oe.name
WHERE oe.year=".$year_report." AND oe.id_department=".$department_report." AND id_cat=".$array_report_generation['id_cat'];

$sql_cost_quarter_3 = "SELECT SUM(oe.cost_quarter_3) FROM `materials` m
INNER JOIN cat c ON c.id = m.id_cat
INNER JOIN ordered_equipment oe ON m.id = oe.name
WHERE oe.year=".$year_report." AND oe.id_department=".$department_report." AND id_cat=".$array_report_generation['id_cat'];

$sql_cost_quarter_4 = "SELECT SUM(oe.cost_quarter_4) FROM `materials` m
INNER JOIN cat c ON c.id = m.id_cat
INNER JOIN ordered_equipment oe ON m.id = oe.name
WHERE oe.year=".$year_report." AND oe.id_department=".$department_report." AND id_cat=".$array_report_generation['id_cat'];

Код получается сильно грамоздким с учетом большого числа условий выбора
 
Сверху