$sub_query = "SELECT t3.`auto_sel_ecu_id` FROM `auto_sel_brand` t1
JOIN `auto_sel_model` t2 ON t2.`auto_sel_model_brand` = t1.auto_sel_brand_id AND t2.`auto_sel_model_name` = '$model'
JOIN `auto_sel_motor` t3 t3.`auto_sel_model_brand` = t2.'auto_sel_brand_id' AND AND `auto_sel_ecu_name` = '$capacity'
WHERE t1.`auto_sel_brand_name` = '$brand' AND t1.`auto_sel_brand_type` = '$type'";
$query = "SELECT `auto_sel_motor_name` FROM `auto_sel_ecu` WHERE `auto_sel_motor_model` IN ('$sub_query')";
$result = $dbc->query($query) or DIE($dbc->error);
....
select
t2.Id,
t2.Name,
t3.Date,
t2.Other
from
(
select
t1.Id,
t1.Name,
t1.Date,
t1.Other
from your_table t1
where not exists (
select 1
from your_table
where Id = t1.Id
and Date > t1.Date
)
) t2 join (
select
Id,
Name,
Date as Date,
Other
from test4
where Other = 555
) t3 on t3.Id = t2.Id
select
t3.user_id,
t4.city_id,
(t3.oper_zayavki / t4.`Заявки`) * t4.`Расход`
from Goroda_Statistika t4
inner join
(
select
t1.user_id,
t2.city_id,
sum(t2.`Заявки`) as oper_zayavki
from Operatory t1
join Operatory_Statistika t2 on t2.user_id = t1.user_id
group by t1.user_id, t2.city_id
) t3 on t3.city_id = t4.city_id