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

customerproducts表:

更新:
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();
}发布于 2016-06-21 11:48:49
如果您希望它作为一个选择:
SELECT cp.customerID,cp.productID,p.name
FROM products p
INNER JOIN customerProducts cp
ON(p.productID = cp.productID)如果要将列添加到第二个表中,请首先添加该列,然后更新:
UPDATE customerProducts cp
SET cp.name = (SELECT p.name FROM products p
WHERE p.productID = cp.productID)https://stackoverflow.com/questions/37943500
复制相似问题