2012-03-06 67 views
-1

鑑於表創建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約翰內斯堡,韋斯特伯裏,房間,出租,說明

將SQL查詢是這個道理,我在使用PostgreSQL更關心。

+0

字面上**所有值的**,像每一個單柱?這是MySQL還是PostgreSQL? – Yuck 2012-03-06 15:48:14

+0

你想在每個表的所有列上做一個'JOIN'?任何你想要這樣做的理由? – Karlson 2012-03-06 15:50:38

+0

@Yuck我正在從使用MySQL轉換到PostgreSQL,所以它會是postgreSQL,那麼從每個表的某些值中,某些表被用作參考表。 – Aaron 2012-03-06 15:50:51

回答

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 
0

你可以這樣來做:

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

檢查這個例子按照烏爾疑問句。

SELECT A.*,B.*,C.* 
FROM A , B, C 
WHERE C.c = B.b 
    AND B.b = A.a 
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 Ans at: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

一般情況下,你正在尋找一個加入..

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 

等等

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 
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;