原来数据库有三个类别表和一个产品表都是用外键关联
表type1中有2个字段 有2条记录userid是会员的主键type1_id,userid  
1          5
2          6表type2中有2个字段 有3条记录type1_id是表type1的idtype2_id,type1_id  
1          2
2          1  
3          1 
 
表type3中有2个字段 有4条记录type2_id是表type2的idtype3_id,type2_id  
1         3
2         2
3         2
4         1  
原产品表p中 有四个字段 有三条记录 0就是没有选子分类
p_id,type1_id,type2_id,type3_id
1       1        0        0
2       2        1        0
3       1        3        1现在想把type1,type2,type3表合为一个类别表category,有四个个字段 p_count的意思是类别下边产品的数量
如果是一级类别all_fatherid值就是,0,自己id,
如果不是一级类别all_fatherid值就是父节点all_fatherid的值自己id,category_id,father_id,all_fatherid,userid,p_count
1            0        ,0,1,        5       2
2            0        ,0,2,        6       1
3            2        ,0,2,3,      6       1
4            1        ,0,1,4,      5       0
5            1        ,0,1,5,      5       1
6            5        ,0,1,5,6,    5       1
7            4        ,0,1,4,7,    5       0
8            4        ,0,1,4,8,    5       0
9            3        ,0,2,3,9,    6       0新的产品表中插入数据 表名product,有四个字段 
category_id存入的是:if(type3_id!=0)
{
  category_id=type3_id;
}
else
{
  if(type2_id!=0)
{
  category_id=type2_id;
}
else
{
 category_id=type1_id;}
}
product_id,category_id,all_category_id,userid
1          1            ,0,1,            5
2          3            ,0,2,3,          6
3          6            ,0,1,5,6,        5

解决方案 »

  1.   

    现在想把type1,type2,type3表合为一个类别表category,有四个个字段 p_count的意思是类别下边产品的数量
    如果是一级类别all_fatherid值就是,0,自己id,
    如果不是一级类别all_fatherid值就是父节点all_fatherid的值自己id,category_id,father_id,all_fatherid,userid,p_count
    1            0        ,0,1,        5       2
    2            0        ,0,2,        6       1
    3            2        ,0,2,3,      6       1
    4            1        ,0,1,4,      5       0
    5            1        ,0,1,5,      5       1
    6            5        ,0,1,5,6,    5       1
    7            4        ,0,1,4,7,    5       0
    8            4        ,0,1,4,8,    5       0
    9            3        ,0,2,3,9,    6       0----------------------------------------------------
    不太明白你是什么意思,这个是通过type1,type2,type3,p这4个表查出来的吗?---------------------------------------------------
    新的产品表中插入数据 表名product,有四个字段,下面的是新产品表?product_id,category_id,all_category_id,userid
    1          1            ,0,1,            5
    2          3            ,0,2,3,          6
    3          6            ,0,1,5,6,        5
      

  2.   

    楼主可以去看下,你发的第一张贴子。因为,是根据t1,t2,t3的数据重新建立一张category表。。
    同时根据p表及上面三表,重新建立product表。
    目前没有想到简单的方法