2016-05-28 14 views
0

クエリに関する質問があります。 二つのテーブルがあります。属性が1つ不足しているクエリ属性

Objects table: 
============== 
Object ID Date 
1  2016-03-15 
2  2016-01-20 

Attributes table: 
================= 
Parent ID Attribute AttributeData 
1  Size  XL 
2  Size  S 
2  Price  20 

The query is to join the data to get this: 
========================================== 
Objet ID Size Price 
1  XL NULL 
2  S 20 

But I only get this: 
========================================== 
Objet ID Size Price 
2  S 20 

LEFT JOINの助けにはならない - ID1のための価格のための属性テーブルにエントリがないので。

このような初心者のために申し訳ありません。

助けを喜んでください。

私は条件付き凝集が何をしたいんだと思うステファン

+0

既存のクエリを投稿してください。これが答えに影響するため、使用しているデータベースを明確にしてください。 – Alex

答えて

2

あなたがどうなる left joinを使用して
select parentid, 
     max(case when attribute = 'Size' then attributedata end) as size, 
     max(case when attribute = 'Price' then attributedata end) as price 
from attributes 
group by parentid; 

:これが動作するためにすることを

select o.*, s.attributedata as size, p.attributedata as price 
from objects o left join 
    attributes s 
    on o.objectid = s.parentid and s.attribute = 'Size' left join 
    attributes p 
    on o.objectid = p.parentid and p.attribute = 'Price'; 

注意、上の条件を属性名はwhere句ではなく、on句にある必要があります。

関連する問題