SQL Server 2008 DBMS

时间:2015-08-04 04:48:20

标签: sql-server sql-server-2008

我需要从两个不同的表中添加两个列值,第二个表将更改并影响数据库中的值。

  • 第一个表格为SendStockByVendor,列数为product_nameQuantity
  • 第二个表格为presentsupply,列数为product_nameQuantity

这两个值在第二个表或数据库中添加和存储。但同样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; 

3 个答案:

答案 0 :(得分:0)

使用UNLOAD ('select_statement') TO 's3://object_path_prefix' [ WITH ] CREDENTIALS [AS] 'aws_access_credentials' [ option [ ... ] ] 来获得无与伦比的LEFT JOIN

Product

答案 1 :(得分:0)

您也可以使用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)

答案 2 :(得分:0)

使用全外连接显示所有匹配和不匹配的输出

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; 
相关问题