Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

´«ÖDz¥¿Í£­Tree in SQL£¨ÒëÎÄ£©£¨3£©

This has some predictable results that we can use for building queries. The root is always of the form (left = 1, right = 2 * (SELECT COUNT(*) from TreeTable)); leaf nodes always have (left + 1 = right); the BETWEEN predicate defines the subtrees; and so on. Here are some common queries that you can use to build others:
ÎÒÃÇ¿ÉÒÔÀûÓÃһЩ¿ÉÔ¤¼ûµÄ½á¹û¹¹½¨²éѯ£¨±í´ïʽ£©¡£¸ù²¿×ÜÊÇÒÔÕâÖÖÐÎʽ¹¹³É£¨left = 1, right = 2 * (SELECT COUNT(*) from TreeTable)£©£»Ò¶×Ó½ÚµãÔò×ÜÊÇ£¨left + 1 = right£©£»BETWEEN±í´ï¶¨ÒåÁË×ÓÊ÷£»µÈµÈ¡£ÕâÀïÓÐһЩ³£ÓõIJéѯ£¬Äã¿ÉÒÔÓÃËüÃÇÀ´×齨¸ü¶àÆäËû£¨²éѯ£©£º
1. Find an employee and all his/her supervisors, no matter how deep the tree.
²éÕÒÒ»¸ö¹ÍÔ±¼°Ëû/ËýµÄËùÓÐÉϼ¶£¬ÎÞÂÛÕâ¿ÅÊ÷½á¹¹ÓжàÉî¡£
SELECT P2.*
from Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P1.emp = :myemployee;
2. Find the employee and all his/her subordinates. (This query has a nice symmetry with the first query.)
²éÕÒ¹ÍÔ±¼°Ëû/ËýËùÓÐÏÂÊô¡££¨Õâ¸ö²éѯÓëµÚÒ»¸ö²éѯÐγÉÁ¼ºÃµÄ¶Ô³Æ¡££©
SELECT P2.*
from Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P2.emp = :myemployee;
3. Add a GROUP BY and aggregate functions to these basic queries and you have hierarchical reports. For example, the total salaries that each employee controls:
¸øÕâЩ»ù±¾²éѯÌí¼ÓGROUP BY£¨¹Ø¼ü×Ö£©ºÍ×ܼƺ¯ÊýÕâÑùÄã¾ÍÓзּ¶¼Ç¼ÁË¡£ÀýÈ磬ÿ¸ö¹ÍÔ±ÄÜÖ§ÅäµÄн³ê×ÜÊý£º
SELECT P2.emp, SUM(S1.salary)
from Personnel AS P1, Personnel AS P2,
Salaries AS S1
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P1.emp = S1.emp
GROUP BY P2.emp;
In the adjacency list method, this has to be done with a cursor.
ÔÚÁÚ½ÓÁбíģʽÀ±ØÐëÒÀ¿¿Óα꣨cursor£©²ÅÄÜÕâÑù×ö¡£
4. Find the level of each node, so you can print the tree as an indented listing.
²éÕÒÿ¸ö½ÚµãµÄ¼¶±ð£¬ÕâÑùÄã¾ÍÄܹ»ÒÔËõ½øÁбíÐÎʽ´òÓ¡Õâ¿ÃÊ÷ÁË¡£
SELECT COUNT(P2.emp) AS indentation, P1.emp
from Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
GROUP BY P1.emp
ORDER BY P1.lft;
5. The nested set mod


Ïà¹ØÎĵµ£º

SQL Server Ë÷Òý½á¹¹¼°ÆäʹÓã¨Ò»£©

Ò»¡¢ÉîÈëdz³öÀí½âË÷Òý½á¹¹
¡¡¡¡Êµ¼ÊÉÏ£¬Äú¿ÉÒÔ°ÑË÷ÒýÀí½âΪһÖÖÌØÊâµÄĿ¼¡£Î¢ÈíµÄsql serverÌṩÁËÁ½ÖÖË÷Òý£º¾Û¼¯Ë÷Òý£¨clustered index£¬Ò²³Æ¾ÛÀàË÷Òý¡¢´Ø¼¯Ë÷Òý£©ºÍ·Ç¾Û¼¯Ë÷Òý£¨nonclustered index£¬Ò²³Æ·Ç¾ÛÀàË÷Òý¡¢·Ç´Ø¼¯Ë÷Òý£©¡£ÏÂÃæ£¬ÎÒÃǾÙÀýÀ´ËµÃ÷һϾۼ¯Ë÷ÒýºÍ·Ç¾Û¼¯Ë÷ÒýµÄÇø±ð£º
¡¡¡¡Æäʵ£¬ÎÒÃǵĺºÓï×ÖµäµÄÕ ......

SQL Server Ë÷Òý½á¹¹¼°ÆäʹÓã¨ËÄ£©

¾Û¼¯Ë÷ÒýµÄÖØÒªÐÔºÍÈçºÎÑ¡Ôñ¾Û¼¯Ë÷Òý
¡¡¡¡ÔÚÉÏÒ»½ÚµÄ±êÌâÖУ¬±ÊÕßдµÄÊÇ£ºÊµÏÖСÊý¾ÝÁ¿ºÍº£Á¿Êý¾ÝµÄͨÓ÷ÖÒ³ÏÔʾ´æ´¢¹ý³Ì¡£ÕâÊÇÒòΪÔÚ½«±¾´æ´¢¹ý³ÌÓ¦ÓÃÓÚ“°ì¹«×Ô¶¯»¯”ϵͳµÄʵ¼ùÖÐʱ£¬±ÊÕß·¢ÏÖÕâµÚÈýÖÖ´æ´¢¹ý³ÌÔÚСÊý¾ÝÁ¿µÄÇé¿öÏ£¬ÓÐÈçÏÂÏÖÏó£º
1¡¢·ÖÒ³ËÙ¶ÈÒ»°ãά³ÖÔÚ1ÃëºÍ3ÃëÖ®¼ä¡£
2¡¢ÔÚ²éѯ×îºóһҳʱ£¬ ......

±¨±ísqlÐĵÃһƪ

×î½üºÜ棬ÓиöÏîÄ¿ÂíÉÏÒªÕб꣬һ¸öÏîÄ¿µÈ׏¤£¬Èô¸ÉËöËéµÄʽøÐÐÖУ¬ÓÐÒ»¶Îʱ¼äû¸üÐÂЩÓÐÓªÑøµÄ¶«Î÷ÁË
˵¸öÌâÍâ»°ÏÈ¡£
½ñÌ쿪»ú×¼±¸°Ñ×òÌìµÄ¶«Î÷debugһϣ¬ºÜϰ¹ßµØÓÒ¼üÏîÄ¿µÄÆô¶¯Îļþ¿ªÊ¼debug£¬»úÆ÷ͻȻÀ¶ÆÁÖØÆô¡£¿ªÊ¼ÒÔΪÓÖÊÇÄÚ´æÔÚ͵͵³¬Æµ£¬¼ì²éÁËÒ»ÏÂbios£¬·¢ÏÖûʲôÎÊÌ⣬ҲûÔõôÔÚÒ⣬ËíÖØÐ¿ªÆôvs2008¼ÌÐø ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ