首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sql数据库中有2个表或外键的内部连接?

sql数据库中有2个表或外键的内部连接?
EN

Stack Overflow用户
提问于 2016-06-21 11:44:12
回答 1查看 4.7K关注 0票数 2

我希望将产品名称从Products表获取到CustomerProducts表。

Products表:

customerproducts表:

更新:

代码语言:javascript
复制
public void bindgrid()
    {
        SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
        SqlCommand cmd = new SqlCommand("select Name From Products p InnerJoin CustomerProducts cp ON(p.ProductID = cp.ProductID)", conn);

        SqlDataAdapter da = new SqlDataAdapter("", conn);
        da.SelectCommand = new SqlCommand("select ProductName From Products p InnerJoin CustomerProducts cp ON(p.ProductID = cp.ProductID", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-21 11:48:49

如果您希望它作为一个选择:

代码语言:javascript
复制
SELECT cp.customerID,cp.productID,p.name
FROM products p
INNER JOIN customerProducts cp
 ON(p.productID = cp.productID)

如果要将列添加到第二个表中,请首先添加该列,然后更新:

代码语言:javascript
复制
UPDATE customerProducts cp
SET cp.name = (SELECT p.name FROM products p
               WHERE p.productID = cp.productID)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37943500

复制
相关文章

相似问题

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