有个商品类别表如下结构:
create table Category
(
id bigint not null auto_increment, //商品id
Cat_id bigint, //商品父类id
cnName varchar(100), //中文名
enName varchar(100), //英文名
LevelNum int, //所在层次
description varchar(500),
primary key (id)
);
现在我想随便输入一个类别id ,想查出其所有子类列表,需要统计出每个子类下的商品数量,我的问题是怎么统计子类下的商品数量,因为子类下边有可能有下一级子类,商品类别6层以上,类似于淘宝
create table Category
(
id bigint not null auto_increment, //商品id
Cat_id bigint, //商品父类id
cnName varchar(100), //中文名
enName varchar(100), //英文名
LevelNum int, //所在层次
description varchar(500),
primary key (id)
);
现在我想随便输入一个类别id ,想查出其所有子类列表,需要统计出每个子类下的商品数量,我的问题是怎么统计子类下的商品数量,因为子类下边有可能有下一级子类,商品类别6层以上,类似于淘宝
解决方案 »
- mysql中删除字段里的书名号
- asp.net 使用MySQL数据库,在SQLyog企业版中文显示乱码?
- Linux下C程序连接MySQL数据出错
- 如何再linux中去找my.cnf的具体位置?
- 请问,win下 Postgresql8.3如何让非本机用户访问?
- 检查表中每个记录是否有字符串"++++++++++" 如果有截取掉这字符串及后面所有字符串 如何做
- 我在MYSQL中的表怎么新建不了
- 【急问】怎么备份和恢复mysql数据库?在php里面可以做吗?
- mysql导出数据到SQL server 2000
- now()可否做建表的默认项?
- mysql 触发器不稳定?
- 帮小弟看个A表数据导入B表的问题
--------------
001
002
003
...二级类别
-------------
001001
001002
001003
...如果要统计 001 类别下的 商品数量的话, 可以在 product 表中
select count(*) from product where category_id like concat('001', '%')
也很容易知道 LevelNum = length(category id) / 3
比如
length('001')/3 = 1.
length('001001') / 3 = 2
用递归把符合条件的类编号写到临时表里,然后select count ……where id in(select id from temptable)
(
id bigint not null auto_increment, //商品id
Cat_code varchar, //商品代号 1-2-3
cnName varchar(100), //中文名
enName varchar(100), //英文名
LevelNum int, //所在层次description varchar(500),
primary key (id)
); 通过code,[-]的个数 来表示商品的层次