简体   繁体   中英

How do I query multiple tables using inner join?

Given the table creation SQL and insertion SQL, How will I generate a query to display the information in aa particular way?

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

Now I want the numerical values that are in the table estate to be displayed by the string values that they represent.

EG 1 Johannesburg, Westbury, Room, Rent, Description

What will the SQL Query be for this, I am more concerned in using postgreSQL.

you cany try like

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

Final

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 

If you want to get info about joins have look to below image

替代文字 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?

First, they must have some sort of common field. Let's assume the common field between them is called <tablename>_ID ; the way you do it is as follows:

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

In general, you are looking for a join..

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

etc...

You could do it this way :

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

Check this example as per ur ques.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM