There are two subqueries. Both return tables of the form:

код_товара | SUM(колво_поступило) код_товара | SUM(колво_продали) 

which are calculated from different tables.

How to combine the results of these subqueries with the table товары :

 код_товара | наименование_товара 

so that you can select in the main query:

 SELECT наименование_товара, (колво_поступило)-(колво_продали) AS осталось FROM товары JOIN --подставить результат подзапросов ... HAVING осталось>0 

?

If we directly substitute the subqueries in the JOIN with the table товары , then phpMyAdmin immediately swears at the invalid syntax of such a JOIN.

  • Give the code with which you "directly subquery subqueries". In theory, a pseudonym should save you. - Ponio

1 answer 1

 SELECT `наименование_товара`, (q1.`колво_поступило`)-(q2.`колво_продали`) AS `осталось` FROM `товары` t1 JOIN (подзапрос_1) AS q1 ON q1.`код_товара` = `t1.код_товара` JOIN (подзапрос_2) AS q2 ON q2.`код_товара` = `t1.код_товара` HAVING `осталось` > 0;