Question : How can i use a subquery within a SELECT?

I know its possible to run a query like:

SELECT
product_main.product_name,
(SELECT SUM(product_extension.product_quantity)
FROM product_main INNER JOIN product_extension ON product_main.product_id = product_extension.product.id
GROUP BY product_main.product_id, product_main.product_location, product_extension.product_quantity
HAVING product_main.product_id = '1234' AND product_main.product_location = 'UK') AS 'Quantity'

FROM product_main
INNER JOIN product_extension ON product_main.product_id = product_extension.product.id

The reason it runs ok is because the subquery returns only 1 result based on my HAVING statement.

However, i'd like to run the query for all products in the tables and not specify only one product_id.
When i run the query without giving a product id i get the error message:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

The results i am after is:
PRODUCT     |  QTY
---------------------
PRODUCT1   |   3
PRODUCT2   |   6
PRODUCT4   |   8

I hope that makes sense.

Answer : How can i use a subquery within a SELECT?

http:#a33193495 would have made a great question!

Select product_main.product_id, product_main.description,
 sum(product_extension.quantity) as total_quantity,
 sum(case when product_extension.location = 'UK' then product_extension.quantity end) as uk_quantity
FROM product_main INNER JOIN product_extension ON product_main.product_id = product_extension.product.id
Group by product_main.product_id, product_main.description
Random Solutions  
 
programming4us programming4us