商品多属性筛选案例

​#商品表

create table goods(

id int not null auto_increment primary key,

goods_name int not null default 0 comment '商品名称',

price int not null default 0 comment '商品价格'

)

insert into goods (goods_name,price)values('苹果手机','5000');

insert into goods (goods_name,price)values('小米手机','8000');

#属性表

create table attr(

id int not null auto_increment primary key,

catid int not null default 0 comment '类型ID',

pid int not null default 0 comment '父ID',

name char(20) not null default '' comment '属性或者规格名称'

)

insert into attr (catid,pid,name)values(1,0,'重量');

insert into attr (catid,pid,name)values(1,1,'15g');

insert into attr (catid,pid,name)values(1,1,'20g');

insert into attr (catid,pid,name)values(1,0,'颜色');

insert into attr (catid,pid,name)values(1,4,'红色');

insert into attr (catid,pid,name)values(1,4,'黑色')

id  catid  pid  name

1     1    0    重量

2     1    1    15g

3     1    1    20g

4     1    0    颜色

5     1    4    红色

6     1    4    黑色


#商品属性保存关联表

create table goods_attr(

id int not null auto_increment primary key,

catid int not null default 0 comment '分类ID',

goodsid int not null default 0 comment '商品ID',

attrid int not null default 0 comment '属性ID',

attr_value_id int not null default 0 comment '属性值ID'

)

第一个商品 参数-重量:15g  颜色:红色

第二个商品 参数-重量:15g  颜色:黑色

id  catid  goodsid  attrid  attr_value_id

1     1      1        1         2

2     1      1        4         5

3     1      2        1         2 

4     1      2        4         6

insert into goods_atrr(catid,goodsid,attrid,attr_value_id)values(1,1,1,2);

insert into goods_atrr(catid,goodsid,attrid,attr_value_id)values(1,1,4,5);

insert into goods_atrr(catid,goodsid,attrid,attr_value_id)values(1,2,1,2);

insert into goods_atrr(catid,goodsid,attrid,attr_value_id)values(1,2,4,6)

我现在要查第一个参数:要求查重量等于15g的商品 ,传入搜索参数 重量属性ID(1)和重量属性值ID (2)

select goodsid from goods_attr where catid = 1 and attrid = 1 and attr_value_id = 2

第一个条件检索 得到商品ID为 1 2 的商品(两个商品)

那下面要再加一个参数:要求查出 重量等于15g的商品  和 颜色等于黑色的商品

首先把重量的属性ID,属性值ID传过去,然后再把颜色属性ID,颜色属性值ID传过去(4,6)

然后去查询(分两次查询,每个属性查询一次):

select goodsid from goods_attr where catid = 1 and  attrid = 1 and attr_value_id = 2

结果商品ID: 1,2

select goodsid from goods_attr where catid = 1 and  attrid = 4 and attr_value_id = 6

结果商品ID: 2

得到两组数据,最后在两组数据里找到两次属性筛选都符合的商品,查出商品信息

select * from goods where id in(1,2) and id in(2) order by id;

结果商品ID: 2


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值