我需要从两个不同的表中添加两个列值,而第二个表将更改并影响到数据库中的值。
SendStockByVendor,列是product_name,Quantitypresentsupply,列是product_name,Quantity两个值添加并存储在第二个表或数据库中。但是相同的product_name是条件,presentsuppy (表名)的剩馀值保持原样。
我运行了以下查询,但它只显示相同的名称、值和数量,但未显示其余的值。
select
p.Product_ID, p.Product_Name, p.Product_Cost,
p.Total_Cost, p.Product_Description, p.Quantity,
s.Quantity,
p.Quantity + s.Quantity as [Total]
from
presentsupply p
join
SendStockByVendor s on p.Product_Name = s.Product_Name; 发布于 2015-08-04 04:50:44
使用LEFT JOIN来获取不匹配的Product:
SELECT
p.Product_ID,
p.Product_Name,
p.Product_Cost,
Total_Cost = p.Product_Cost * (p.Quantity + ISNULL(s.Quantity, 0)),
p.Product_Description,
p.Quantity,
s.Quantity,
p.Quantity + ISNULL(s.Quantity, 0) AS [Total]
FROM
presentsupply p
LEFT JOIN
SendStockByVendor s ON p.Product_Name = s.Product_Name;发布于 2015-08-04 05:04:05
您还可以使用LEFT OUTER JOIN
SELECT p.Product_ID, p.Product_Name, p.Product_Cost, p.Product_Cost * (p.Quantity + ISNULL(s.Quantity, 0)) AS Total_Cost, p.Product_Description, p.Quantity,
s.Quantity AS Expr1, p.Quantity + ISNULL(s.Quantity, 0) AS Total
FROM presentsupply AS p LEFT OUTER JOIN
SendStockByVendor AS s ON p.Product_Name = s.product_name要存储到一个新表中:创建一个列表,以便保存上面的结果。之后,使用下面的语法!
insert into tablename values (above_query)
发布于 2015-08-04 05:17:10
使用完全外部连接来显示所有匹配和不匹配的输出。
select
p.Product_ID, p.Product_Name, p.Product_Cost,
p.Total_Cost, p.Product_Description, p.Quantity,
s.Quantity,
p.Quantity + s.Quantity as [Total]
from
presentsupply p
Full Outer join
SendStockByVendor s on p.Product_Name = s.Product_Name; https://stackoverflow.com/questions/31800899
复制相似问题