Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用“连接方式”显示数据的父级?_Sql_Oracle_Recursion_Hierarchy_Connect By - Fatal编程技术网

Sql 使用“连接方式”显示数据的父级?

Sql 使用“连接方式”显示数据的父级?,sql,oracle,recursion,hierarchy,connect-by,Sql,Oracle,Recursion,Hierarchy,Connect By,我有这张桌子 parentitem | childitem ---table name dinner | steak dinner | wine dinner | mashed potato dinner | coffee coffee | sugar coffee | water dinner | cake

我有这张桌子

parentitem    |    childitem     ---table name
  dinner      |      steak
  dinner      |      wine
  dinner      |      mashed potato
  dinner      |      coffee
  coffee      |      sugar
  coffee      |      water
  dinner      |      cake
  cake        |      liquid syrup
  cake        |      egg
我想在使用ff代码之前使用connect by检索“晚餐”的所有子项

 Select  Level, LPAD('->',2*(LEVEL-1))||CHILDITEM     From table  
 Start With parentitem = 'dinner'    Connect By Prior childitem =
 parentitem
但它不包括“晚餐”这一父项,但它正确地生成了晚餐的所有子项,顺便说一句,我的朋友暗示我使用union。我正在使用oracle

所以我的预期结果是

LEVEL  |   CHILDITEM
  0    |  dinner
  1    |  steak
  1    |  wine
  1    |  mashed potato
  1    |  coffee
  2    |  sugar
  2    |  water
  1    |  cake
  2    |  liquid syrup
  2    |  egg

问题是您的数据中没有将“晚餐”作为子项

如果您这样做了,那么只需
从childitem='dinner'开始

例如:

与数据一样,如果希望将“晚餐”列为子项,则需要通过查询创建一个不存在的行
UNION-ALL
是一种很好的方法。例如:

SELECT 0 AS "LEVEL",
       'dinner' childitem
FROM   DUAL
UNION ALL
SELECT LEVEL,
       LPAD ('->', 2 * (LEVEL - 1)) || childitem
FROM   t
START WITH parentitem = 'dinner'
CONNECT BY PRIOR childitem = parentitem
另一种方法是使用
UNION ALL
创建源数据中缺少的行。例如:

SELECT LEVEL,
       LPAD ('->', 2 * (LEVEL - 1)) || childitem
FROM   (SELECT NULL parentitem,
               'dinner' childitem
        FROM   DUAL
        UNION ALL
        SELECT parentitem,
               childitem
        FROM   t) t
START WITH childitem = 'dinner'
CONNECT BY PRIOR childitem = parentitem

问题是您的数据中没有将“晚餐”作为子项

如果您这样做了,那么只需
从childitem='dinner'开始

例如:

与数据一样,如果希望将“晚餐”列为子项,则需要通过查询创建一个不存在的行
UNION-ALL
是一种很好的方法。例如:

SELECT 0 AS "LEVEL",
       'dinner' childitem
FROM   DUAL
UNION ALL
SELECT LEVEL,
       LPAD ('->', 2 * (LEVEL - 1)) || childitem
FROM   t
START WITH parentitem = 'dinner'
CONNECT BY PRIOR childitem = parentitem
另一种方法是使用
UNION ALL
创建源数据中缺少的行。例如:

SELECT LEVEL,
       LPAD ('->', 2 * (LEVEL - 1)) || childitem
FROM   (SELECT NULL parentitem,
               'dinner' childitem
        FROM   DUAL
        UNION ALL
        SELECT parentitem,
               childitem
        FROM   t) t
START WITH childitem = 'dinner'
CONNECT BY PRIOR childitem = parentitem