´«Ö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²©´ó¾«ÉÄãÃÇ»áÓõ½µÄ¡£——mAysWINd
Ò»¸öÌâÄ¿Éæ¼°µ½µÄ50¸öSqlÓï¾ä
Student(S#,Sname,Sage,Ssex) ѧÉú±í
Course(C#,Cname,T#) ¿Î³Ì±í
SC(S#,C#,score) ³É¼¨±í
Teacher(T#,Tname) ½Ìʦ±í
ÎÊÌ⣺
1¡¢²éѯ“001”¿Î³Ì±È“002”¿Î³Ì³É¼¨¸ßµ ......
1.²éÕÒÖØ¸´Êý¾Ý±íµÄidÒÔ¼°Öظ´Êý¾ÝµÄÌõÊý
select max(id) as nid,count(id) as ÖØ¸´ÌõÊý from tableName
group by linkname Having Count(*) > 1
2.²éÕÒÖØ¸´Êý¾Ý±íµÄÖ÷¼ü
select max(id) as nid from tableName
group by linkname Having Count(id) > 1
3.ɾ³ýÖØ¸´µÄÊý¾Ý
delete from table ......
--µÃµ½Êý¾Ý¿âµÄËùÓÐÕßÃû³Æ
SELECT distinct RDB$OWNER_NAME AS DATABASE_OWNER
from RDB$RELATIONS
WHERE (RDB$SYSTEM_FLAG = 1);
--¸ù¾Ý±íÃûµÃµ½±íµÄÖ÷¼ü
SELECT RC.RDB$CONSTRAINT_NAME AS CONSTRAINT_NAME,
I.RDB$RELATION_NAME AS TABLE_NAME,
S.RDB$FIELD_NAME AS COLUMN_NAME
from RDB$RELATION_CONSTRAINTS ......
¸ÄÉÆSQLÓï¾ä
¡¡¡¡ºÜ¶àÈ˲»ÖªµÀSQLÓï¾äÔÚsql serverÖÐÊÇÈçºÎÖ´Ðеģ¬ËûÃǵ£ÐÄ×Ô¼ºËùдµÄSQLÓï¾ä»á±»SQL SERVERÎó½â¡£±ÈÈ磺
select * from table1 where name=''zhangsan'' and tID > 10000
ºÍÖ´ÐÐ:
select * from table1 where tID > 10000 and name=''zhangsan''
¡¡¡¡Ò»Ð©È˲»ÖªµÀÒÔÉÏÁ½ÌõÓï¾äµÄÖ´ÐÐЧÂÊÊÇ·ñÒ» ......
×î½üºÜ棬ÓиöÏîÄ¿ÂíÉÏÒªÕб꣬һ¸öÏîÄ¿µÈ׏¤£¬Èô¸ÉËöËéµÄʽøÐÐÖУ¬ÓÐÒ»¶Îʱ¼äû¸üÐÂЩÓÐÓªÑøµÄ¶«Î÷ÁË
˵¸öÌâÍâ»°ÏÈ¡£
½ñÌ쿪»ú×¼±¸°Ñ×òÌìµÄ¶«Î÷debugһϣ¬ºÜϰ¹ßµØÓÒ¼üÏîÄ¿µÄÆô¶¯Îļþ¿ªÊ¼debug£¬»úÆ÷ͻȻÀ¶ÆÁÖØÆô¡£¿ªÊ¼ÒÔΪÓÖÊÇÄÚ´æÔÚ͵͵³¬Æµ£¬¼ì²éÁËÒ»ÏÂbios£¬·¢ÏÖûʲôÎÊÌ⣬ҲûÔõôÔÚÒ⣬ËíÖØÐ¿ªÆôvs2008¼ÌÐø ......