查询需要链接到两个表之间 [英] Query needed to link between 2 tables
问题描述
表1
Purchased_Items
包含:
Item_ID:编号
Item_Quantity:Number
表2
Sold_Items
包含:
Item_ID:编号
Item_Quantity:Number
我如何在(Item_Quantity)中的两个表之间求和,而没有内部联接,如果位于表1中但不在表2中的项应该显示一个值,反之亦然,则应该处理这种情况在表1中
请帮助.
Table 1
Purchased_Items
Contains :
Item_ID:Number
Item_Quantity:Number
Table 2
Sold_Items
Contains :
Item_ID:Number
Item_Quantity:Number
How can I make a summation between two tables in (Item_Quantity), without inner join , these is a case should be handled if an item located in table 1 but not in table 2 , the is a value should be shown and vice versa , which is in table 1
Please help .
推荐答案
如果您想购买未售出的已购买商品,请使用以下查询:
If you would like to get purchased items which are not sold, use this query:
SELECT *
FROM Purchased_Items
WHERE NOT Item_ID IN (SELECT Item_ID FROM Sold_Items)
如果您想对两个表的Item_Quantity求和:
If you would like to sum Item_Quantity for both tables:
SELECT 'Sum Of Purchased_Items' As [Item Description], SUM(Item_Quantity) AS [Item Value]
FROM Purchased_tems
UNION ALL
SELECT 'Sum Of Sold_Items' As [Item Description], SUM(Item_Quantity) AS [Item Value]
FROM Purchased_tems
输出示例:
商品说明 | 商品价值 |
---|---|
购买商品总和 | 5,000.00 |
已售商品总和 | 2,000.00 |
Example output:
Item Description | Item Value |
---|---|
Sum Of Purchased_Items | 5,000.00 |
Sum Of Sold_Items | 2,000.00 |
这篇关于查询需要链接到两个表之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!