Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The query is this

SELECT foods.food_id,foods.foodname,foods.foodownername,foods.balance,imports.import_quantity,exports.export_quantity 
FROM foods 
JOIN (SELECT food_id,SUM(quantity) import_quantity FROM imports GROUP BY food_id) imports ON imports.food_id=foods.food_id
JOIN (SELECT food_id,SUM(quantity) export_quantity FROM exports GROUP BY food_id) exports ON exports.food_id=foods.food_id

I wanted to have a query to filter by food name, so if I search a foodname -in search bar and it's available in stock - it displays foodid, foodname, foodownername, balance in stock, imported quantity and exported quantity.

What I have tried:

SELECT * from foods where foodname='$_POST[foodname]

but when i use this quey it only displays foodid,foodname,foodownername and balance but import quantity and export quantity is not diplayed
Posted
Updated 24-Jun-22 1:58am
Comments
Richard Deeming 24-Jun-22 4:54am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
Richard Deeming 24-Jun-22 4:56am    
Beyond that, if you want to include data from joined tables in your filtered query, you need to actually include the joins in your filtered query.
SELECT ... FROM foods JOIN ... WHERE foods.foodname = %s

1 solution

See the comments from @Richard-Deeming for the solution to your immediate problem.

You need to review how you are querying your data overall - if you have a food item that has no imports or exports, or only has imports, or only has exports, then that food item will never appear in your query because you are using INNER joins to your sub-queries.

For example if you insert the following data
SQL
insert into @foods (food_id, foodname, foodownername, balance) values
(1, 'Rice','Joe',200),
(2, 'Flour','Jane',100),
(3, 'Tea','Jack',800),
(4, 'Oil', 'Jennifer',1000);
insert into @imports (food_id, quantity) values
(1, 150),
(2, 250),
(3, 350);
insert into @exports (food_id, quantity) values
(1, 175),
(2, 275),
(4, 555);
Then your query will return
food_id	foodname	foodownername	balance	import_quantity	export_quantity
1		Rice		Joe				200		150				175
2		Flour		Jane			100		250				275
Whereas you probably wanted to see
food_id	foodname	foodownername	balance	import_quantity	export_quantity
1		Rice		Joe				200		150				175
2		Flour		Jane			100		250				275
3		Tea			Jack			800		350				0
4		Oil			Jennifer		1000	0				555
There is also no need for using sub-queries if you restructure your query differently, and there is every chance it will perform better without the sub-queries. E.g.
SQL
SELECT f.food_id,f.foodname,f.foodownername,f.balance
	,ISNULL(SUM(i.quantity),0) as import_quantity
	,ISNULL(SUM(e.quantity),0) as export_quantity 
FROM foods f
LEFT OUTER JOIN imports i ON i.food_id = f.food_id
LEFT OUTER JOIN exports e on e.food_id = f.food_id
GROUP BY f.food_id,f.foodname,f.foodownername,f.balance
(Caveat: This is MSSQL not MySQL so you may need to change the ISNULL function to the MySQL equivalent)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900