Oracle start with ... connect by prior Ó÷¨
Óï·¨£º
select *
from ±íÃû
where Ìõ¼þ1
start with Ìõ¼þ2
connect by prior µ±Ç°±í×Ö¶Î=¼¶Áª±í×Ö¶Î
start withÓëconnect by priorÓï¾äÍê³ÉµÝ¹é¼Ç¼£¬ÐγÉÒ»¿ÃÊ÷Ðνṹ£¬Í¨³£¿ÉÒÔÔÚ¾ßÓвã´Î½á¹¹µÄ±íÖÐʹÓá£
start with±íʾ¿ªÊ¼µÄ¼Ç¼
connect by prior Ö¸¶¨Ó뵱ǰ¼Ç¼¹ØÁªÊ±µÄ×ֶιØϵ
´úÂ룺
--´´½¨²¿ÃÅ±í£¬ÕâÊÇÒ»¸ö¾ßÓвã´Î½á¹¹µÄ±í£¬×ӼǼͨ¹ýparent_idÓ븸¼Ç¼µÄid½øÐйØÁª
create table DEPT(
ID NUMBER(9) PRIMARY KEY, --²¿ÃÅID
NAME VARCHAR2(100), --²¿ÃÅÃû³Æ
PARENT_ID NUMBER(9) --¸¸¼¶²¿ÃÅID£¬Í¨¹ý´Ë×Ö¶ÎÓëÉϼ¶²¿ÃŹØÁª
);
Ïò±íÖвåÈëÈçÏÂÊý¾Ý£¬ÎªÁËʹ´úÂë¼òµ¥£¬Ò»¸ö²¿ÃŽö¾ßÓÐÒ»¸öϼ¶²¿ÃÅ
¡ñ´Ó¸ù½Úµã¿ªÊ¼²éѯµÝ¹éµÄ¼Ç¼
select *
from dept
start with id=1
connect by prior id = parent_id;
ÏÂÃæÊDzéѯ½á¹û£¬start with id=1±íʾ´Óid=1µÄ¼Ç¼¿ªÊ¼²éѯ£¬ÏòÒ¶×ӵķ½ÏòµÝ¹é£¬µÝ¹éÌõ¼þÊÇid=parent_id£¬µ±Ç°¼Ç¼µÄidµÈÓÚ×ӼǼµÄparent_id
¡ñ´ÓÒ¶×ӽڵ㿪ʼ²éѯµÝ¹éµÄ¼Ç¼
select *
from dept
start with id=5
connect by prior parent_id = id;
ÏÂÃæÊDzéѯ½á¹û£¬µÝ¹éÌõ¼þ°´ÕÕµ±Ç°¼Ç¼µÄparent_idµÈÓ븸¼Ç¼µÄid
¡ñ¶Ô²éѯ½á¹û¹ýÂË
select *
from dept
where name like '%ÏúÊÛ%'
start with id=1
connect by prior id = parent_id;
ÔÚÏÂÃæµÄ²éѯ½á¹ûÖпÉÒÔ¿´µ½£¬Ê×ÏÈʹÓÃstart with... connect by prior²éѯ³öÊ÷ÐεĽṹ£¬È»ºówhereÌõ¼þ²ÅÉúЧ£¬¶ÔÈ«²¿²éѯ½á¹û½øÐйýÂË
¡ñpriorµÄ×÷ÓÃ
prior¹Ø¼ü×Ö±íʾ²»½øÐеݹé²éѯ£¬½ö²éѯ³öÂú×ãid=1µÄ¼Ç¼£¬ÏÂÃæÊǽ«µÚÒ»¸ö²éѯȥµôprior¹Ø¼ü×Öºó½á¹û
select *
from dept
start with id=1
connect by prior id = parent_id;
Ïà¹ØÎĵµ£º
ËäȻѧϰJavaºÜ¾ÃÁË£¬×Ô¼ºÒ²Á¬½Ó¹ýһЩÊý¾Ý¿â£¬±ÈÈçmysqlÖ®ÀàµÄ£¬Èç½ñÄØ£¬Ò²Ñ§Ï°ÁËÒ»¶Îʱ¼äµÄOracle£¬È»¶øÄØ£¬½ñÌìÊÇÎÒµÚÒ»´ÎÁ¬½ÓOracle£¬ºÙºÙ£¬Ó¦¸Ã»¹²»ËãÌ«³Ù°É¡£
½ñÌìÄØ£¬Óе㱿׾£¬´ó¼ÒĪЦ£¡
ÎÒÕâÊÇÒ»¸ö²éѯÀý×Ó
Ê×ÏÈ£¬Ô ......
ºÃ¼¸´ÎÏëÓõ½ÍâÁ¬½ÓµÄʱºò¶¼ÍüÁ˾ßÌåµÄÓ÷¨ÊÇÔõÑùµÄ£¬±ÈÈç˵£¨+£©¸Ã¼ÓÔڵȺŵÄÄÄÒ»¶Ë£¬»òÕßLFET OUTER JOIN¸ÃÓÃÔÚÕûÌõÓï¾äÖеÄÄĸö²¿·Ö¡£½ñÌìÕýºÃÓÖÅöµ½Ò»¸öÏà¹ØµÄÎÊÌ⣬½è´Ë»ú»á×ܽáһϣ¬ÒÔºóÒ²·½±ã²éѯ£¬²»ÓÃÿ´Î¶¼È¥°Ù¶ÈÁË¡£
//table1ºÍtable2ΪÁ½¸ö²âÊÔ±í Ëæ±ã²åÈ뼸ÌõÊý¾Ý
SQL> select * from table1;
......
£¨1£© Ñ¡Ôñ×îÓÐЧÂʵıíÃû˳Ðò(Ö»ÔÚ»ùÓÚ¹æÔòµÄÓÅ»¯Æ÷ÖÐÓÐЧ)£º
ORACLEµÄ½âÎöÆ÷°´ÕÕ´ÓÓÒµ½×óµÄ˳Ðò´¦Àífrom×Ó¾äÖеıíÃû£¬from×Ó¾äÖÐдÔÚ×îºóµÄ±í(»ù´¡±í driving table)½«±»×îÏÈ´¦Àí£¬ÔÚfrom×Ó¾äÖаüº¬¶à¸ö±íµÄÇé¿öÏÂ,Äã±ØÐëÑ¡Ôñ¼Ç¼ÌõÊý×îÉٵıí×÷Ϊ»ù´¡±í¡£Èç¹ûÓÐ3¸öÒÔÉϵıíÁ¬½Ó²éѯ, ÄÇ ......
¾Û¼¯(cluster)ÊÇ´æ´¢±íÊý¾ÝµÄ¿ÉÑ¡ÔñµÄ·½·¨¡£Ò»¸ö¾Û¼¯ÊÇÒ»×é±í£¬½«¾ßÓÐͬһ¹«¹²ÁÐÖµµÄÐд洢ÔÚÒ»Æ𣬲¢ÇÒËüÃǾ³£Ò»ÆðʹÓá£ÕâЩ¹«¹²Áй¹³É¾Û¼¯Âë¡£
¾³£±»Í¬Ê±·ÃÎʵıíÔÚÎïÀíλÖÃÉÏ¿ÉÒÔ´æ´¢ÔÚÒ»Æð¡£ÎªÁ˽«ËüÃÇ´æ´¢ÔÚÒ»Æ𣬾ÍÒª´´½¨Ò»¸ö´Ø( c l u s t e r )À´¹ÜÀíÕâЩ±í¡£±íÖеÄÊý¾ÝÒ»Æð´æ´¢ÔÚ´ØÖУ¬´Ó¶ø×îС»¯±ØÐëÖ´ÐеÄI ......