Here is the result table images.I want to print out these available units using sum function.
1)查询1号
SELECT SUM(inventory.inn-inventory.out_inv) AS available_units
FROM products
LEFT JOIN inventory on inventory.product_id=products.product_id
GROUP BY inventory.product_id
2)查询2号
SELECT products.product_manual_id,products.product_name,products.product_description,products.product_unit,product_categories.category_name,products.product_image,price.cost ,price.selling_price
FROM products,stores,product_taxes ,price,product_categories
WHERE (products.store_id=2 AND product_taxes.tax_id=products.tax_id AND price.product_id=products.product_id OR product_categories.category_id=products.category_id) AND (products.product_id <=31 AND products.product_id >=29 )
GROUP By products.product_name ORDER BY products.product_name ASC
我想结合这两个问题&#39;结果。我该如何解决?我一直在尝试很多选择。无论谁给我答案,我都会感激。提前谢谢。
答案 0 :(得分:0)
您可以使用product_id
两个选择表连接 select t1.*, t2.* from (
SELECT
products.product_id
, SUM(inventory.inn - inventory.out_inv) AS available_units
FROM products
LEFT JOIN inventory on inventory.product_id=products.product_id
GROUP BY inventory.product_id
) t1
left join (
SELECT
products.product_manual_id
,products.product_id
,products.product_name
,products.product_description
,products.product_unit
,product_categories.category_name
,products.product_image
,price.cost
,price.selling_price
FROM products,stores,product_taxes ,price,product_categories
WHERE (products.store_id=2
AND product_taxes.tax_id=products.tax_id
AND price.product_id=products.product_id
OR product_categories.category_id=products.category_id)
AND (products.product_id <=31 AND products.product_id >=29 )
GROUP By products.product_name
) t2 on t1.product_id = t2.product_id
ORDER BY t2.product_name ASC