gpt4 book ai didi

mysql - 将两个查询结果合并为一个结果

转载 作者:行者123 更新时间:2023-11-30 22:05:39 24 4
gpt4 key购买 nike

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

我想合并这两个查询的结果。我该如何解决?我一直在尝试很多选择。谁给我一个答案,我将不胜感激。提前致谢。

最佳答案

您可以通过 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

关于mysql - 将两个查询结果合并为一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41858988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com