如何使用内部联接查询多个表?

时间:2012-03-06 15:46:37

标签: mysql sql postgresql

鉴于表创建SQL和插入SQL,我将如何生成查询以特定方式显示信息?

CREATE TABLE cities (
   id serial NOT NULL UNIQUE PRIMARY KEY,
   iname varchar(100) NOT NULL UNIQUE
)

CREATE TABLE suburbs (
    id serial NOT NULL UNIQUE PRIMARY KEY,
    icity integer REFERENCES cities (id), 
    iname varchar(100) NOT NULL UNIQUE
)
CREATE TABLE type (
   id serial NOT NULL UNIQUE PRIMARY KEY,
   iname varchar(100) NOT NULL UNIQUE
)
CREATE TABLE sale (
   id serial NOT NULL UNIQUE PRIMARY KEY,
   iname varchar(100) NOT NULL UNIQUE
)

CREATE TABLE estate (
    id serial NOT NULL UNIQUE PRIMARY KEY,
    icity integer REFERENCES cities (id),
    isuburb integer REFERENCES suburbs (id),
    itype integer REFERENCES type (id),
    isale integer REFERENCES sale (id),
    idescription text,
    itimestamp timestamp DEFAULT CURRENT_TIMESTAMP
)

INSERT INTO cities (iname) VALUES ('Johannesburg');
INSERT INTO suburbs (icity, iname) VALUES (1, 'Westbury');
INSERT INTO type (iname) VALUES ('Room');
INSERT INTO sale (iname) VALUES ('Rent');

INSERT INTO estate (icity, isuburb, itype, isale, idescription) VALUES (1, 1, 1, 1, 'A Nice place in a real bad neighbor hood');

现在我希望表格中的数值以它们代表的字符串值显示。

E.G 1 Johannesburg,Westbury,房间,租,描述

SQL Query将是什么,我更关心使用postgreSQL。

7 个答案:

答案 0 :(得分:15)

你不能尝试

select * from table1  
inner join table2 on tabl1.pk = table2.FK 
inner join table3 on tabl1.pk = table.FK 

最终

select table2.iname,table3.iname,table4.iname,table1.idescription 
from estate  as table1 
inner join sale  as table2 on table1.isale = table2.id 
inner join  type  as table3 on table1.itype  = table3.id 
inner join  suburbs  as table3 on table1.isuburb = table3.id 
inner join  cities as table4 on table1.icity = table4.id 

如果您想获得有关联接的信息,请查看以下图片

alt text 并在:How do I decide when to use right joins/left joins or inner joins Or how to determine which table is on which side?

答案 1 :(得分:6)

首先,他们必须拥有某种共同领域。我们假设它们之间的公共字段称为<tablename>_ID;你这样做的方式如下:

select A.colx, A.coly, A.colz, B.colx, B.colw, c.cold 
from A inner join B on A.ID=B.A_ID 
     inner join C on C.A_ID=A.ID

答案 2 :(得分:2)

select suburbs.icity
        , cities.iname
        , suburbs.iname
        , type.iname
        , sale.iname
        , estate.idescription
from estate
inner join suburbs on suburbs.id = estate.isuburb
inner join cities on cities.id = estate.icity
inner join type on type.id = estate.itype
inner join sale on sale.id = estate.isale

答案 3 :(得分:1)

一般情况下,您正在寻找加入..

select ct.iname as CityName,sb.name as SuburbName,et.*
from estate et
join cities ct on ct.id=et.icity
join suburbs sb on sb.id=et.isuburb

等...

答案 4 :(得分:0)

你可以这样做:

SELECT *
FROM A
INNER JOIN B 
ON B.id = A.b
INNER JOIN C
ON C.id = A.c

答案 5 :(得分:0)

根据你的问题检查这个例子。

SELECT A.*,B.*,C.*
FROM A , B, C
WHERE C.c = B.b
  AND B.b = A.a

答案 6 :(得分:0)

 -> select * from estate
 -> inner join cities on estate.icity=cities.id
 -> inner join suburbs on estate.isuburb=suburbs.id
 -> inner join type on estate.itype=type.id
 -> inner join sale on estate.isale=sale.id;