Database design, three-tier classification list

Database Design of three categories
such as the type comprising 0> Type 1 Type 1 comprises> Type 2
Type 0 -> Type 1 -> 2 Type
Type of database fields 0   id = 1 parentId = 0
database field 1 of the type    id = 2 parentId = 1
database field type 3   id = 3 parentId = 2
e.g.

Because type 0 ID = 1 of the next stage so that queries can be classified according to the type 1 the parentId a = 1 ( 1 here is the type 0 of the ID ) condition

Query the Type 1 result set then check the type 3 can be according parentId type 3 (= 2 where 2 is the type of a query result set classification of the third layer of the id).

To sum it up: The value of the id field of the current category, as parentId field under a category of values

The following case study

the first sort

sql statement is    the SELECT  *  from tb_category WHERE the parent_id = ' 0 ' ;

The second category

SELECT * from tb_category where parent_id='1';

The third category

SELECT * from tb_category where parent_id='2';

The advantage of this is that newly added types can be added id the upper classification as the value of parent_id.

Guess you like

Origin www.cnblogs.com/july7/p/12046303.html