选择语句以检索ID的代码

时间:2019-01-08 23:10:52

标签: sql postgresql

我在表id_org中有3列(code_orgid_root_orgorg)。 id_root_org指定表中某个组织的最高组织。

org 1 (id_org=1, id_root_org = 1 since it is its own root)
  --org 2 (id_org=2, id_root_org = 1)
  --org 3 (id_org=3, id_root_org = 1)
    --org 3.1 (id_org=3.1, id_root_org = 1)

如何检索所有根组织的代码?

2 个答案:

答案 0 :(得分:1)

看来,根是id_root_org = id_org所在的记录。要获得该行的code_org,您可以使用简单的SELECT

SELECT code_org
       FROM elbat
       WHERE id_root_org = id_org;

答案 1 :(得分:0)

您只想要ID_ROOT_ORG的列表?

SELECT DISTINCT ID_ROOT_ORG
FROM [TABLE_NAME]