日期:2014-05-17  浏览次数:20813 次

关于Oracle的题,各位帮帮忙了。。。


要求:根据图书分类表,更新图书登记表中黄色区域信息

各位 抽空帮帮忙了!!!不胜感激啊。。。
oracle

------解决方案--------------------
没有想到好办法,写一个递归函数?
------解决方案--------------------
 with categories as(
  2      select 1 id, 'literature
------解决方案--------------------
history' categoryname, null fatherid from dual
  3       union all
  4      select 2 id, 'lang' name, 1 fatherid from dual
     union all
    select 9 id, 'English' name, 2 fatherid from dual
),
books as (
    select 1 id, 'CET4' bookname, 9 categoryid from dual
     union all
    select 2 id, 'Chinese' bookname, 2 categoryid from dual
)
select t1.id bookid, t1.bookname, t2.categoryid
  from books t1, 
  (select id, sys_connect_by_path(categories.id, '/') categoryid
    from categories
   start with categories.fatherid is null  
connect by prior categories.id = categories.fatherid) t2
 where t1.categoryid = t2.id;   5    6    7    8    9   10   11   12   13   14   15   16   17   18   19  

    BOOKID BOOKNAM CATEGORYID
---------- ------- --------------------------------------------------
         2 Chinese /1/2
         1 CET4    /1/2/9


搞成这样先,后面的字符串,你用SUBSTR,INSTR那样的函数截下。
------解决方案--------------------
引用:
with categories as(
  2      select 1 id, 'literature
------解决方案--------------------
history' categoryname, null fatherid from dual
  3       union all
  4      select 2 id, 'lang' name, 1 fatherid from dual
     ……

3喽正解