2017-03-23 9 views
0

編集ポスト上の制約に基づいて、異なるテーブルからのデータの表示:PostgreSQLの - テーブルの1

だから私は、以下の次のテーブルを持って、私はUniMember、テーブルからの電話からフィールド名を表示しようとしていますAflの終了フィールドがnullであるという制約に基づいて、テーブルAflのスタッフと開始フィールド。私は非常に新しいPostgresqlであり、基本的には2日間これに固執しています。私は多くの異なるクエリを試みましたが、表示することができませんでした。

期待される出力は、次の行に沿っている必要があります。

名前|電話|開始

ありがとう!

create table UniMember (
    id   integer, -- PG: serial 
    unswid  integer unique, -- staff/student id (can be null) 
    password ShortString not null, 
    family  LongName, 
    given  LongName not null, 
    title  ShortName, -- e.g. "Prof", "A/Prof", "Dr", ... 
    sortname LongName not null, 
    name  LongName not null, 

    primary key (id) 
); 

create table Staff (
    id   integer references People(id), 
    office  integer references Rooms(id), 
    phone  PhoneNumber, -- full number, not just extension 
    primary key (id) 
); 

create table Afl (
    staff  integer references Staff(id), 
    orgUnit  integer references OrgUnits(id), 
    role  integer references Staff_roles(id), 
    isPrimary boolean, -- is this role the basis for their employment? 
    starting date not null, -- when they commenced this role 
    ending  date, -- when they finshed; null means current 
    primary key (staff,orgUnit,role,starting) 
); 
+0

試した試行されたクエリと共にテーブル構造を投稿する必要があります。 –

+0

* **あなたの質問に** [EDIT] **し、そのデータに基づいていくつかのサンプルデータと予想される出力を追加してください。 [**フォーマットされたテキスト**](http://stackoverflow.com/help/formatting)、[スクリーンショットなし](http://meta.stackoverflow.com/questions/285551/why-may-i-not -upload-images-of-code-on-so-ask-a-question/285557#285557) –

答えて

0

理想的にすべてのテーブルを参照する必要がありますが、あなたが一緒にテーブルをリンクして、必要なデータを表示するには、テーブル(プライマリおよびセカンダリキー)との間で、共通のフィールドを必要とし、内部結合に探してみてください。

0

ビューを作成して、データを適切に出力することができます。あなたは以下のようにそれを行うことができます。

create view test as select name, phone, starting 
from unimember a, staff b, afl c 
where b.id = c.staff 

今、あなたは、ビュー上で選択することができます。出力の

Select * from test; 

より明確な定義は、あなたが質問を次回素晴らしいことです。

大丈夫

+0

ありがとうございました。私はこれを試しましたが、出力が長すぎて表示されていません。通常はかなり時間がかかりますか? – runswmily

関連する問題