Victor
Victor

Reputation: 13

Joining tables in Postgres

I have a table...

col1  | id   | col3|
---------------------
 1    |123   |     |
 2    |456   |     |
 3    |789   |     |

And I have another table...

id  | name |
------------
123 | Tom  |
456 | Kate |
789 | Pork |
101 |Winter|
102 |Roll  |

I want to join the tables together to get a result that looks like this...

col1  | id   | col3| name
----------------------------
 1    |123   |     | Tom
 2    |456   |     | Kate
 3    |789   |     | Pork

Can someone help me please?

Thanks in advance

Upvotes: 0

Views: 467

Answers (3)

VrushM
VrushM

Reputation: 21

If you only want the data where id from one table matches to id at another table then you can do an inner join like this:

SELECT * FROM table1 INNER JOIN table2 ON table2.id = table1.id

If you want all the data from the first table but only matched id data from the second table then you can do this:

SELECT * FROM table1 LEFT JOIN JOIN table2 ON table2.id = table1.id

For more information about Joins you an refer to this link - SQL joins

Upvotes: 1

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

Do the JOIN :

SELECT t1.*, t2.name
FROM table1 t1 INNER JOIN
     table2 t2
     ON t1.id = t2.id;

Upvotes: 0

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 32003

use inner join between two tables

select col1,table1.id,col3,name from table1
inner join table2 on table1.id =table2.id

Upvotes: 0

Related Questions