Using the SQL Traversal loop query, if you do not consider using CET, the estimated time to build their own wheels, now think that the SQL CET is really a good thing, for SQL recursive query, is a good way;
withEtcRecommandINfo2 (C_precommendno,c_recommendname,c_recommendno,i_rtype,c_serverdir,c_pic_recommend,tlevel) as( SelectC_precommendno,c_recommendname,c_recommendno,i_rtype,c_serverdir,c_pic_recommend,0 asTlevel fromRecommendwhereC_recommendno='RC20150514142804' Union All SelectRd.c_precommendno,rd.c_recommendname,rd.c_recommendno,rd.i_rtype,rd.c_serverdir,rd.c_pic_recommend,et.tlevel+1 fromRecommend RdInner JoinEtcRecommandINfo2 ET onEt.c_precommendno=Rd.c_recommendno)SelectC_precommendno,c_recommendname,c_recommendno,i_rtype,c_serverdir,c_pic_recommend,tlevel fromEtcRecommandINfo2;
After reading this code, you may be drunk, in fact, this code, mainly used in CET, first of all, this use to query this table:
C_recommendno |
Identifies a unique in the table |
C_precommendno |
Identifies the previous piece of data for this piece of data |
This is the main 2 fields, and these two fields are the root of the recursion.
This statement can be understood in 3 steps, such as:
1, query the basic data;
2, the basic data related conditions, query all data;
3, the query data, through with the classification display processing;
*************************************************************
**************************************************************
NOTE: Statement 2 in the following statement is also very important, = the order of the left and right is not interchangeable, after the interchange, the results of the query is not the same; remember the order of understanding is key, understand the order, you can query the results you want;
For more information on CET, refer to the link:
Http://www.cnblogs.com/kissdodog/archive/2013/06/24/3153012.html
SQL Query CET use comprehension