简体   繁体   中英

Joining two SQL tables

I have two database tables with some common IDs. This is the structure of the first table:

ID 1 - House 1 - New york - 0100 xxx xxx
ID 2 - House 2 - London - 0100 yyx yyx

This is the structure of the second table:

ID 101 - House 1 - Human 1 - 0100 yyy yyy
ID 102 - House 1 - Human 2 - 0100 xyx yxy
ID 103 - House 2 - Human 3 - 0100 xxy xxy

I would like to find a way to join the two tables together to result in something similar to the table below:

House 1 - New York - 0100 xxx xxx - Human 1 - 0100 yyy yyy, Human 2 - 0100 xyx yxy

UPDATE:

This is current query:

SELECT a.SO as ID, a.NOB as house, a.TEL as tel, o.IME_PREZIME as person,     o.MOBILNI_TELEFON as tel2
FROM OBJEKAT a
join OBJEKAT_KONTAKTOSOBA o on o.SO=a.SO
where a.MOBILNI_TELEFON is not null and o.MOBILNI_TELEFON is not null
order by 2

And this is what i get (two or more rows for each human, and i want it under one row with multiple columns)

16  01NI    018.522.885     Marija Radosavljević    064.844.1858
17  02NI    018.243.568     Dragana Grujić  064.844.1833
17  02NI    018.243.568     Adriana Cvetković   064.844.1867
15  04SK    021.465.263     Nikolina Lulić  064.844.1860
15  04SK    021.465.263     Dragana Borovčanin  064.844.1852
12  05KA    024.877.077     Milena Lončar   064.844.1724

As you can see, under ID 17 and 15 I have two rows and I want It to be one like I show you in first explanation.

THIRD EDIT: What i want is this:

17  02NI    018.243.568     Dragana Grujić  064.844.1833  Adriana Cvetković 064.844.1867

FOURTH EDIT: This is not duplicate, above duplicate suggestions is for mysql, and i have Firebird, whis doesn't have GROUP_CONCAT function

Your question is not totally clear. But from what I understand, what you need is a foreign key on table2 that references a primary key on table1, something like table2.person_id(fk) references table1.id(primary) .

Then your sql query would be something like :

"SELECT * FROM table1,table2 ON table1.id = table2.person_id WHERE ..."

hope this helps

If you have following column names:

CREATE TABLE Table1
(
  Id INT,
  HouseName NVARCHAR(60),
  City NVARCHAR(60),        
  Col4 NVARCHAR(60)  
);
CREATE TABLE Table2
(
  Id INT,
  HouseName NVARCHAR(60),
  HumanName NVARCHAR(60),        
  Col4 NVARCHAR(60)  
);
INSERT INTO Table1 VALUES 
(1, 'House 1', 'New york', '0100 xxx xxx'),
(2, 'House 2', 'London', '0100 yyx yyx');

INSERT INTO Table2 VALUES 
(101, 'House 1', 'Human 1', '0100 yyy yyy'),
(102, 'House 1', 'Human 2', '0100 xyx yxy'),
(103, 'House 2', 'Human 3', '0100 xxy xxy');

SELECT t1.HouseName, t1.City, t1.Col4, t2.HumanName, t2.Col4, t3.HumanName, t3.Col4
FROM Table1 t1
JOIN Table2 t2
   ON t1.HouseName = t2.HouseName
JOIN Table2 t3
   ON t2.Id < t3.Id
WHERE t2.HouseName = t3.HouseName

SQL FIDDLE

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