´«Ö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:
using System.Data.SqlClient;
string sql = "server=.;uid=sa;pwd=;database=tablename;";
ACCESS:
using System.Data.OleDb;
string sql = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + HttpRuntime.AppDomainAppPath + "//App_Data//db.mdb";
HttpRuntime.AppDomainAppPath Ϊ¸ùĿ¼
......
´Ó×òÌ쿪ʼ¾ÍÒ»Ö±ÔÚÍøÉϲéÕÒÊý¾Ý¿âΪSQL server 2000 µÄJSP¿ª·¢»·¾³µÄ´î½¨£¬µ½ÏÖÔÚΪֹÖÕÓÚÄܹ»Õý³£Áª½Ó²¢·ÃÎÊ¡£ÒÔÏÂÊǴµÄ²½Ö裺
1¡¢´ÓÍøÉÏÏÂÔØÒ»¸öSQL server 2000 ¸öÈ˰æµÄ°²×°°ü£¬°´ÕÕÌáʾ²Ù×÷£¬µ±ÔÚÉí·ÖÑé֤ʱ£¬Ñ¡Ôñ»ìºÏģʽ£¬Óû§saµÄÃÜÂë¿ÉÒÔΪ¿Õ£¬Ò²¿ÉÒÔ²»Îª¿Õ¡£
2¡¢´ÓÍøÉÏÏÂÔØÒ»¸öSQ ......
SQL Server 2005Ïà¶ÔÓÚSQL Server 2000À´Ëµ£¬ÎÞÂÛÊÇÐÔÄÜ»¹Êǹ¦Äܶ¼ÓÐÒ»¸öÏ൱´óµÄÌá¸ß,ÉõÖÁ¿ÉÒÔÓÓ¸ïÃü”À´ÐÎÈÝÕâÒ»´ÎÉý¼¶¡£SQL Server 2005ʹ SQL Server õÒÉíÓÚÆóÒµ¼¶Êý¾Ý¿âÐÐÁС£ÔÚÊý¾Ý¸ß¿ÉÓÃÐÔ·½Ã棬SQL Server 2005ΪÓû§ÌṩÁËÊý¾Ý¾µÏñ¡¢¸´ÖÆ¡¢¹ÊÕÏ×ªÒÆÈº¼¯¡¢ÈÕÖ¾´«Ë͹¦ÄÜ¡£±¾ÎÄÏò¶ÁÕß¼òµ¥½é½áSQL Serve ......
windows ¼¯³ÉÑéÖ¤£º
<connectionStrings>
<add name="ConnectionStr" connectionString="Data Source=CAIPENG-PC;database=Test;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
</connectionStrings>
»òÕß
<connectionStrings>
<add name=" ......
SQL×¢Èë¹¥»÷µÄΣº¦ÐԺܴó¡£ÔÚ½²½âÆä·ÀÖ¹°ì·¨Ö®Ç°£¬Êý¾Ý¿â¹ÜÀíÔ±ÓбØÒªÏÈÁ˽âÒ»ÏÂÆä¹¥»÷µÄÔÀí¡£ÕâÓÐÀûÓÚ¹ÜÀíÔ±²ÉÈ¡ÓÐÕë¶ÔÐԵķÀÖδëÊ©¡£
¡¡ Ò»¡¢ SQL×¢Èë¹¥»÷µÄ¼òµ¥Ê¾Àý¡£
¡¡¡¡statement := "SELECT * from Users WHERE Value= " + a_variable + "
ÉÏÃæÕâÌõÓï¾äÊÇºÜÆ ......