P
P
pr0kazn1k2015-01-15 12:04:32
PostgreSQL
pr0kazn1k, 2015-01-15 12:04:32

Query against many tables with PostgreSQL conditions?

It is necessary to unite the data from N tables, and communication on different fields. It turns out to write simple queries with JOIN, but there is not enough knowledge. What is necessary.

Таблица №1.
а | b
-----
1 | A
2 | B
3 | C

Таблица №2.
b | c
-------
A | 3F
B | 4F
D | 5F

Таблица №3.
c  | d
-------
3F | t1
6F | t2
7F | t3

Таблица №4.
c  | e
-------
1F | u1
4F | u2
7F | u3

Хотелось бы получить
a | b | c  | d  | e
-------------------
1 | A | 3F | t1 | -
2 | B | 4F |  - | u2
3 | C |  - |  - | -

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
krypt3r, 2015-01-15
@pr0kazn1k

Focus on this query

SELECT t1.a, t1.b, t2.c, t3.d, t4.e
FROM t1
LEFT JOIN t2 ON (t1.b = t2.b)
LEFT JOIN t3 ON (t2.c = t3.c)
LEFT JOIN t4 ON (t2.c = t4.c)
ORDER BY t1.a

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question