2017-02-18 5 views
1

I次の2つの表があります。SQL:問題入社テーブル

私は(例:396)のitem_idを供給するときというクエリを記述できるようにする必要があり
attributevalueassign 

itm_id attr_value_id attr_id 
----- ------------- ------- 
396  237    1 
396  20    2 
715  274    1 
715  16    2 

attributevalue 

attr_id attr_value_id  attr_value 
------- -------------  ---------- 
1   237    3055020 
1   274    2454518 
2   16    Dunlop 
2   20    Vogue 

、私は戻って、次の結果が得られます。

SELECT  dbo.select_attributevalue.attr_value, dbo.select_attributevalueassign.itm_id 
FROM   dbo.select_attributevalueassign INNER JOIN 
         dbo.select_attributevalue ON dbo.select_attributevalueassign.attr_id = dbo.select_attributevalue.attr_id 
WHERE  (dbo.select_attributevalueassign.itm_id = 396) 

itm_id attr_value_id attr_id attr_value 
------ ------------- ------- ---------- 
396  237    1  3055020 
396  237    1  2454518 
396  20    2  Dunlop 
396  20    2  Vogue 

だから、私の考えはその私はおそらく笙です:私はINNER JOINを使用しようとすると、私は私が望むよりも多くの行を取得

itm_id attr_value_id attr_id attr_value 
------ ------------- ------- ---------- 
396  237    1   3055020 
396  20    2   Vogue 

私はJOINを使用していませんが、私はその代替案が何であるか分かりません。私はこのサイトで解決策を探していましたが、結合を使用して重複レコードを削除する方法については多くの質問がありますが、私が求めていたのと似たものは見当たりませんでした。私のSQLクエリの知識は基本的なものなので、何をすべきかはわかりません。どんな助けでも大歓迎です!

答えて

2

2つの列attr_value_idattr_idで内部結合を行う必要があります。このクエリを試してみてください:

SELECT  dbo.select_attributevalue.attr_value, dbo.select_attributevalueassign.itm_id 
FROM   dbo.select_attributevalueassign 
    INNER JOIN dbo.select_attributevalue ON 
    dbo.select_attributevalueassign.attr_id = dbo.select_attributevalue.attr_id 
    AND dbo.select_attributevalueassign.attr_value_id = dbo.select_attributevalue.attr_value_id 
WHERE  (dbo.select_attributevalueassign.itm_id = 396) 
+0

素晴らしいです!ありがとう! –