简体   繁体   English

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

[英]sql query oracle to fit a tree structure format

I have created these sample tables to create a json tree structure so that i can use jqtree to create a tree layout.我创建了这些示例表来创建 json 树结构,以便我可以使用 jqtree 创建树布局。

I want my json to be in the format我希望我的 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"}

]

The tables are as follows.表格如下。

| 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               |

I am assuming it would be hierarchical query, i have never written one before, i need some help with that.我假设它是分层查询,我以前从未写过一个,我需要一些帮助。 I don't know where to start and how to start it.我不知道从哪里开始以及如何开始。

EDIT编辑

One of the comments in the answers is that the schema design is not clear.答案中的评论之一是架构设计不清楚。 What changes should I do to make to get the data in the json format, so that it maintains the hierarchy我应该做哪些更改以获取json格式的数据,以便它保持层次结构

EDIT2编辑2

My current query returns this table我当前的查询返回此表

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

The JSON you are proposing appears to have no correlation between the ID s you are assigning and the ID s in the tables this will make it difficult to connect anything from the client-side back to the database.你提议似乎有没有相关性的JSON ID就是你要分配和ID S上的表中,这将使其难以从客户端回数据库连接任何东西。

You would be better re-organising your tables so that you can put everything into a single hierarchical structure.您最好重新组织您的表格,以便您可以将所有内容放入一个单一的层次结构中。 Something like a Linnaean Taxonomy:有点像林奈分类法:

SQL Fiddle SQL小提琴

Oracle 11g R2 Schema Setup : 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;

Then you can extract the data relatively simply:然后你可以相对简单地提取数据:

Query 1 - Get everything taxonomically related to "Cat" :查询 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 |

Query 2 - Get everything taxonomically related to "Canine" :查询 2 - 获取与“犬”分类学相关的所有内容

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 |

Here's an example of a hierarchical query from the Oracle documentation:以下是 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 http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm

Something like this in your case but your schema design isn't clear在你的情况下是这样的,但你的架构设计不清楚

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM