sql查询oracle以适合树结构格式

时间:2015-06-25 08:27:38

标签: sql oracle hierarchical

我已创建这些示例表以创建json树结构,以便我可以使用jqtree创建树布局。

我希望我的json格式为

[
  {"id":1, "parentid": 0, "name": "Carnivores"},
  {"id":2, "parentid": 0, "name": "Herbivores"},
  {"id":3, "parentid": 1, "name": "Dogs"},
  {"id":4, "parentid": 3, "name": "Labradors"},  
  {"id":5, "parentid": 3, "name": "Pugs"},
  {"id":6, "parentid": 3, "name": "Terriers"}

]

表格如下。

| catg_id       |   catg_name       | 
| —————-        |————————-          |
| 1             |   Carnivores      |
| 2             |   Herbivores      |



| animal_catg_id    | animal_catg_name      |   catg_id |
| —————-        |————————-                  |————————-  |
| 1             |   Dogs                    |   1       |
| 2             |   Cats                    |   1       |
| 3             |   Cows                    |   2       |
| 4             |   Buffalo                 |   2       |



| animal_id     | animal_name   | animal_catg_id    |
| —————-        |————————-      |   ————————-       |
| 1             |   labs        |   1               |
| 2             |   pugs        |   1               |
| 3             |   terriers    |   1               |
| 4             |   german      |   1               |
| 5             |   lion        |   2               |
| 6             |   tiger       |   2               |

我假设它将是分层查询,我之前从未写过,我需要一些帮助。 我不知道从哪里开始以及如何开始它。

修改

答案中的一个评论是架构设计不明确。 我应该做些什么来获取json格式的数据,以便它维护层次结构

EDIT2

我当前的查询返回此表

Carnivores     |  Dogs    | labs
Carnivores     |  Dogs    | pugs
Carnivores     |  Dogs    | terriers
.......

2 个答案:

答案 0 :(得分:1)

您提议的JSON似乎与您分配的ID和表中的ID之间没有相关性,这将使从客户端返回的任何内容难以连接数据库。

您最好重新组织表格,以便将所有内容放入单个层次结构中。像Linnaean Taxonomy的东西:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE Taxonomies ( ID, PARENT_ID, Category, Taxonomy, Common_Name ) AS
          SELECT  1, CAST(NULL AS NUMBER),  'Kingdom',     'Animalia',    'Animal'       FROM DUAL
UNION ALL SELECT  2,  1,    'Phylum',      'Chordata',    'Chordate'     FROM DUAL
UNION ALL SELECT  3,  2,    'Class',       'Mammalia',    'Mammal'       FROM DUAL
UNION ALL SELECT  4,  3,    'Order',       'Carnivora',   'Carnivore'    FROM DUAL
UNION ALL SELECT  5,  4,    'Family',      'Felidae',     'Feline'       FROM DUAL
UNION ALL SELECT  6,  5,    'Genus',       'Panthera',    'Tiger'         FROM DUAL
UNION ALL SELECT  7,  5,    'Genus',       'Felis',       'Cat'           FROM DUAL
UNION ALL SELECT  8,  5,    'Genus',       'Lynx',        'Lynx'          FROM DUAL
UNION ALL SELECT  9,  4,    'Family',      'Canidae',     'Canid'        FROM DUAL
UNION ALL SELECT 10,  9,    'Genus',       'Canis',       'Canine'       FROM DUAL
UNION ALL SELECT 11, 10,    'Species',     'Canis Lupus', 'Gray Wolf'     FROM DUAL
UNION ALL SELECT 12, 11,    'Sub-Species', 'Canis Lupus Familiaris', 'Domestic Dog' FROM DUAL
UNION ALL SELECT 13, 12,    'Breed',       NULL,          'Pug'           FROM DUAL
UNION ALL SELECT 14, 12,    'Breed',       NULL,          'German Shepherd' FROM DUAL
UNION ALL SELECT 15, 12,    'Breed',       NULL,          'Labradors'     FROM DUAL
UNION ALL SELECT 16,  7,    'Species',     'Felis Catus', 'Domestic Cat'  FROM DUAL
UNION ALL SELECT 17,  8,    'Species',     'Lynx Lynx',   'Eurasian Lynx' FROM DUAL
UNION ALL SELECT 18,  8,    'Species',     'Lynx Rufus',  'Bobcat'        FROM DUAL;

然后您可以相对简单地提取数据:

查询1 - 获取与" Cat" 的分类相关的所有内容:

SELECT *
FROM (
  SELECT *
  FROM   Taxonomies
  START WITH Common_Name = 'Cat'
  CONNECT BY PRIOR PARENT_ID = ID
  ORDER BY LEVEL DESC
)
UNION
SELECT *
FROM (
  SELECT *
  FROM   Taxonomies
  START WITH Common_Name = 'Cat'
  CONNECT BY PRIOR ID = PARENT_ID
  ORDER SIBLINGS BY Common_Name
)

<强> Results

| ID | PARENT_ID | CATEGORY |    TAXONOMY |  COMMON_NAME |
|----|-----------|----------|-------------|--------------|
|  1 |    (null) |  Kingdom |    Animalia |       Animal |
|  2 |         1 |   Phylum |    Chordata |     Chordate |
|  3 |         2 |    Class |    Mammalia |       Mammal |
|  4 |         3 |    Order |   Carnivora |    Carnivore |
|  5 |         4 |   Family |     Felidae |       Feline |
|  7 |         5 |    Genus |       Felis |          Cat |
| 16 |         7 |  Species | Felis Catus | Domestic Cat |

查询2 - 获取与分类相关的所有内容&#34; Canine&#34;

SELECT *
FROM (
  SELECT *
  FROM   Taxonomies
  START WITH Common_Name = 'Canine'
  CONNECT BY PRIOR PARENT_ID = ID
  ORDER BY LEVEL DESC
)
UNION
SELECT *
FROM (
  SELECT *
  FROM   Taxonomies
  START WITH Common_Name = 'Canine'
  CONNECT BY PRIOR ID = PARENT_ID
  ORDER SIBLINGS BY Common_Name
)

<强> Results

| ID | PARENT_ID |    CATEGORY |               TAXONOMY |     COMMON_NAME |
|----|-----------|-------------|------------------------|-----------------|
|  1 |    (null) |     Kingdom |               Animalia |          Animal |
|  2 |         1 |      Phylum |               Chordata |        Chordate |
|  3 |         2 |       Class |               Mammalia |          Mammal |
|  4 |         3 |       Order |              Carnivora |       Carnivore |
|  9 |         4 |      Family |                Canidae |           Canid |
| 10 |         9 |       Genus |                  Canis |          Canine |
| 11 |        10 |     Species |            Canis Lupus |       Gray Wolf |
| 12 |        11 | Sub-Species | Canis Lupus Familiaris |    Domestic Dog |
| 13 |        12 |       Breed |                 (null) |             Pug |
| 14 |        12 |       Breed |                 (null) | German Shepherd |
| 15 |        12 |       Breed |                 (null) |       Labradors |

答案 1 :(得分:0)

以下是Oracle文档中的分层查询示例:

SELECT last_name, employee_id, manager_id, LEVEL
  FROM employees
  START WITH employee_id = 100
  CONNECT BY PRIOR employee_id = manager_id
  ORDER SIBLINGS BY last_name;

http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm

在你的情况下这样的东西,但你的架构设计不清楚

 SELECT animal_name, level
  FROM animals
  START WITH parentid is null
  CONNECT BY PRIOR id = parentid;