2016-06-21 31 views
2

ProductsテーブルからCustomerProductsテーブルへの製品名を取得したいと考えています。SQLデータベース内の2つのテーブルまたは外部キーとの内部結合?

Productsテーブル:

Products

customerproductsテーブル:

customerproducts

UPDATE:

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(); 
    } 
+0

リンクが動作しません。 –

+2

内部結合と外部キーは異なる目的のための異なるツールです。あなたの質問はバイクや台所ナイフに尋ねるようなものです。いくつかの研究をしてください。 –

+0

そのテーブルのデータでは、期待される結果は何ですか? – jarlh

答えて

2

は、あなたが選択するようにしたい場合:

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) 
+0

更新プログラムを参照してください。 –

+0

私のクエリで何が問題になっていますか?私は更新後に新しい質問は表示されません。 – sagi

+0

私に「InnerJoin」の近くに不正な構文があります。 –

関連する問題