2

同じ ID 名を持つ 2 つのテーブルがあり (テーブルの設計方法を変更することはできません)、テーブル 2 の ID をクエリしようとしています。

create table table1(
    id          integer, -- PG: serial
    description MediumString not null,
    primary key (id)
);


create table table2 (
    id          integer, -- PG: serial
    tid         references table1(id),
    primary key (id)
);

したがって、基本的にそれらが結合されると、次のクエリを実行すると、2 つの列に同じ名前の「id」が付けられます

select * from table1
join table2 on table1.id = table2.tid;
4

3 に答える 3

6

両方の「ID」が必要な場合は、列にエイリアスを設定します

SELECT table1.id AS id1, table2.id AS id2
FROM table1...
于 2012-04-22T08:19:00.020 に答える
2

両方のテーブルですべての * をクエリしたいが、特定の ID を参照できる場合は、それも可能です。おそらく使用しない重複した ID 列になりますが、状況によっては、本当にすべてが必要な場合がありますデータ、それだけの価値があります。

select table1.*, table2.*, table1.id as 'table1.id', table2.id as 'table2.id'
from ...
于 2016-06-21T17:02:03.773 に答える
0

では選択できませんselect *。これを試して :

select table1.id, table1.description, table2.id, table2.tid
from table1
inner join table2
 on table1.id = table2.tid
于 2012-04-22T03:53:41.703 に答える