Skip to content

sql – how to count rows in sub-query according main query items

I’m trying to get a list like this:

qtyAvailable is the SUM() of stockItems rows where stock_items.stock_part_id = stock_parts.id (main query)

By using something like this:

How can I relate the WHERE clause in the sub-query to the main query row?

Advertisement

Answer

Here is a small demo

User contributions licensed under: CC BY-SA
10 People found this is helpful