A
A
Arseny Sokolov2015-06-14 07:14:25
PostgreSQL
Arseny Sokolov, 2015-06-14 07:14:25

How to execute queries with data output with comparison conditions inside a table in PostgreSQL?

Here is a table with the following content:

idnameparent_id
oneTest 010
2Test 020
3Test 030
4Sub test 01one
5Sub test 02one
6Sub test 03one

As you can see from the example, records with ID 4-6 are child items of record with ID 1.
So I can’t figure out how to build a query so that as a result of issuing I have the following table:
idnameparent_idparent
oneTest 010NULL
2Test 020NULL
3Test 030NULL
4Sub test 01oneTest 01
5Sub test 02oneTest 01
6Sub test 03oneTest 01

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Naves, 2015-06-14
@ArsenBespalov

Select tbl1.*, tbl2.name as parent
From mytable as tbl1
Left outer join mytable as tbl2 on tbl1.parent=tbl2.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question