select
t1.id,
t1.sum_ordered - ifnull(t2.sum_sold, 0)
from (select id, sum(amount) as sum_ordered from ordered group by id) t1
left join (select id, sum(amount) as sum_sold from sold group by id) t2 on t2.id = t1.id
SELECT *
FROM logRF
where fio like IFNULL('%".$_POST['fio']."%', fio)
AND b_date = IFNULL('%".$_POST['b_date']."%', b_date)
AND city = IFNULL('%".$_POST['city ']."%', city)
HAVING COUNT(DISTINCT type) = 1