首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Server 2008 DBMS

Server 2008 DBMS
EN

Stack Overflow用户
提问于 2015-08-04 04:48:20
回答 3查看 52关注 0票数 1

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

  • 第一个表是SendStockByVendor,列是product_nameQuantity
  • 第二个表是presentsupply,列是product_nameQuantity

两个值添加并存储在第二个表或数据库中。但是相同的product_name是条件,presentsuppy (表名)的剩馀值保持原样。

我运行了以下查询,但它只显示相同的名称、值和数量,但未显示其余的值。

代码语言:javascript
复制
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; 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-04 04:50:44

使用LEFT JOIN来获取不匹配的Product

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2015-08-04 05:04:05

您还可以使用LEFT OUTER JOIN

代码语言:javascript
复制
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)

票数 0
EN

Stack Overflow用户

发布于 2015-08-04 05:17:10

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

代码语言:javascript
复制
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; 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31800899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档