テーブルにいくつかのエンティティがあり、それらの属性と値が別のテーブルにあります。すべてのエンティティの特定の属性の値を確認できる選択を作成するか、その属性が欠落している場合は null を作成したいと思います。標準SQLを使用してこれを行うにはどうすればよいですか?
これはセットアップです:
create table person (id int not null, nick varchar(32) not null);
insert into person (id, nick) values (1, 'John');
insert into person (id, nick) values (2, 'Peter');
create table req_attributes (name varchar(32));
create table person_attributes (id int not null,
person_id int not null,
attribute varchar(32) not null,
value varchar(64) not null);
insert into person_attributes values (1, 1, 'age', '21');
insert into person_attributes values (2, 1, 'hair', 'brown');
insert into person_attributes values (3, 2, 'age', '32');
insert into person_attributes values (4, 2, 'music', 'jazz');
そして、これは私の現在の選択ステートメントです:
select * from person join person_attributes on
person.id = person_attributes.person_id
where attribute = 'hair';
ピーターの髪に関する情報がないため、明らかにピーターは結果セットに含まれていません。彼も結果セットに入れたいと思いますが、値は null です。
結果セットが次のような場合に最適です
Person, Hair color
John, brown
Peter, null
できればサブクエリは避けたいのですが、ジョインが無理なら大歓迎です。