- PR -

外部結合?

1
投稿者投稿内容
未記入
会議室デビュー日: 2005/02/11
投稿数: 1
投稿日時: 2005-02-11 17:17
PostgreSQL 7.4.3を使ってアプリケーションを作成しているのですが、分からない事があったので質問いたします。外部結合で実現できるかと思ったのですが、上手く行きませんでした。

以下のようなテーブルがあります。
create table customer(
customer_id integer,
customer_name varchar(100));

insert into customer values (1, '客1');
insert into customer values (2, '客2');
insert into customer values (3, '客3');

create table shop(
shop_id integer,
shop_name varchar(100));

insert into shop values (1, '店1');
insert into shop values (2, '店2');
insert into shop values (3, '店3');

あと、shopとcustomerの関係を表すテーブルが2つあります。
create table status1(
customer_id integer,
shop_id integer,
status1 integer);

insert into status1 values (1, 1, 1);
insert into status1 values (1, 2, 2);
insert into status1 values (2, 3, 1);

create table status2(
customer_id integer,
shop_id integer,
status2 integer);

insert into status2 values (2, 1, 1);
insert into status2 values (2, 2, 2);
insert into status2 values (2, 3, 1);

このSQLを実行すると
select customer_name, shop_name, status1 from customer c, shop s, status1 st1
where st1.customer_id = c.customer_id and st1.shop_id = s.shop_id;
以下の結果を得ます。
customer_name | shop_name | status1
---------------+-----------+---------
客1 | 店1 | 1
客1 | 店2 | 2
客2 | 店3 | 1

ここまではOKなんですけど、次にこういう結果を取得したいのですが、どのようなSQLを書けばよいのか分かりません。
customer_name | shop_name | status1 | status2
---------------+-----------+---------+--------
客1 | 店1 | 1 |
客1 | 店2 | 2 |
客2 | 店3 | 1 | 1

宜しくお願いします。

[ メッセージ編集済み 編集者: 未記入 編集日時 2005-02-11 17:30 ]
1

スキルアップ/キャリアアップ(JOB@IT)