请教一个关于通过产品属性筛选产品的sql语句写法

ylmdy 2011-09-26 09:16:40
field value product_id
颜色 红 1
颜色 黄 1
颜色 红 2
尺码 35 1
尺码 36 1
尺码 37 1
尺码 35 2
尺码 36 2

数据结构如上简述,想求得能通过产品属性来筛选出产品ID(product_id)的一条sql语句写法。

如,
颜色:红、尺码:37 =》 产品ID:2
颜色:红、尺码:35 =》 产品ID:1、2

不理解的跟帖哦,在线等待。
...全文
159 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
睡羊羊老婆 2011-09-26
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([field] varchar(4),[value] varchar(2),[product_id] int)
insert [tb]
select '颜色','红',1 union all
select '颜色','黄',1 union all
select '颜色','红',2 union all
select '尺码','35',1 union all
select '尺码','36',1 union all
select '尺码','37',1 union all
select '尺码','35',2 union all
select '尺码','36',2


select * from tb

create function sel_ID(@coler varchar(10),@size varchar(10)) returns table

as return
(
select a.field as afield, a.value as coler, b.field as bfield,b.value as size,a.[product_id] as id
from tb as a join tb as b on a.[product_id]=b.[product_id]
where a.field='颜色' and a.value=@coler and b.field='尺码' and b.value=@size
)


select * from dbo.sel_ID('红','35')

feihongluori 2011-09-26
  • 打赏
  • 举报
回复
use students3;
go
if object_id('product')is not null
drop table product;
go
create table product
(field char(4),value varchar(2),product_id int)
insert into product
select '颜色','红',1 union all
select '颜色','黄',1 union all
select '颜色','红',2 union all
select '尺码','35',1 union all
select '尺码','36',1 union all
select '尺码','37',1 union all
select '尺码','35',2 union all
select '尺码','36',2;
go
declare @color as varchar(2)
declare @size as varchar(2)
set @color='红'
set @size='35'
;
with c_color as--定义一个公用表存放field为‘颜色’的信息
(
select field as color ,value as corvalue,product_id from product where field='颜色'
),
c_size as--再定义一个公用表存放以field为‘尺寸’的信息
(
select field as size,value as sizevalue,product_id from product where not field='颜色'
)
select c2.product_id,color,corvalue,size ,sizevalue from c_color as c1 inner join c_size as c2
on c1.product_id=c2.product_id--两个表求笛卡尔积,可以得出同一个ID所匹配的所有颜色和尺寸
where corvalue=@color and sizevalue=@size--根据输入的颜色尺寸求出id号
/*product_id color corvalue size sizevalue
----------- ----- -------- ---- ---------
1 颜色 红 尺码 35
2 颜色 红 尺码 35

(2 行受影响)
*/
go
drop table product
ylmdy 2011-09-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 qianjin036a 的回复:]

MSSQL里这样做,MYSQL里也应该没什么不同.试一试吧:SQL code
create table tb(field nvarchar(10),value nvarchar(10),product_id int)
insert into tb select '颜色','红',1
insert into tb select '颜色','黄',1
insert into tb select '……
[/Quote]


SELECT distinct(p.product_id),p.product_name,p.special_price,p.price,p.image_default,p.images,p.image_colors
FROM `category_product` `cp`,`attribute` `a` ,`product` `p`
WHERE cp.category_id = 50008899 AND cp.product_id = p.product_id
AND a.type_id = p.product_id AND a.vid in(27370,28314) AND p.status = 1 AND p.stocks > 0
ORDER BY `sort` DESC LIMIT 0,20


qianjin036a兄弟,麻烦帮我看下这条语句。其中的“a.vid in(27370,28314)”vid就是这张attribute属性表的另外一个字段,属性ID,对应field。

现在这条sql语句没起到预期的作用,因为“a.vid in(27370,28314)”这边的in导致查询出来的产品列表是或者的关系(有属性ID是27370或者有属性ID28314的产品)而非有属性ID 27370并且有28314的产品。

不知道是否描述清楚了..谢谢qianjin036a。期待您的再次回复。 :)


-晴天 2011-09-26
  • 打赏
  • 举报
回复
MSSQL里这样做,MYSQL里也应该没什么不同.试一试吧:
create table tb(field nvarchar(10),value nvarchar(10),product_id int)
insert into tb select '颜色','红',1
insert into tb select '颜色','黄',1
insert into tb select '颜色','红',2
insert into tb select '尺码','35',1
insert into tb select '尺码','36',1
insert into tb select '尺码','37',1
insert into tb select '尺码','35',2
insert into tb select '尺码','36',2
go
select distinct a.product_id
from tb a inner join tb b on a.product_id=b.product_id
where a.field='颜色' and a.value='红' and b.field='尺码' and b.value in('37','35')
/*
product_id
-----------
1
2

(2 行受影响)
*/
go
drop table tb
ylmdy 2011-09-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qianjin036a 的回复:]

SQL code
create table tb(field nvarchar(10),value nvarchar(10),product_id int)
insert into tb select '颜色','红',1
insert into tb select '颜色','黄',1
insert into tb select '颜色','红',2
insert into tb selec……
[/Quote]
[Quote=引用 6 楼 fredrickhu 的回复:]

SQL code
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-26 09:29:47
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 1……
[/Quote]

谢谢qianjin036a和fredrickhu的耐心回复。 :D

想再请教下,如果我不管value这字段的值是多少,只想确认出拥有 颜色属性、尺码属性、...的产品ID,那Mysql语句是如何写的? 小弟愚笨,烦请详解。谢谢。 :D
ylmdy 2011-09-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xuchaofeng 的回复:]

楼主最好描述得不够清楚, 怎样的对应关系?
[/Quote]

这张属性表的结构的字段关键有:属性名、属性值、属性对应的ID。

一个产品的多个属性都保存在这张属性表,然后想通过这些属性来筛选出产品ID。

而且是并列的关系,比如:红色并且尺码是35的产品这样。
--小F-- 2011-09-26
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-26 09:29:47
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([field] varchar(4),[value] varchar(2),[product_id] int)
insert [tb]
select '颜色','红',1 union all
select '颜色','黄',1 union all
select '颜色','红',2 union all
select '尺码','35',1 union all
select '尺码','36',1 union all
select '尺码','37',1 union all
select '尺码','35',2 union all
select '尺码','36',2
--------------开始查询--------------------------
declare @value varchar(10)
set @value='37'
select
a.field,a.value,b.field,b.value,b.product_id
from
tb a join tb b on a.product_id=b.product_id
where
a.field='颜色' and a.value='红' and b.field='尺码' and b.value=@value
----------------结果----------------------------
/*field value field value product_id
----- ----- ----- ----- -----------
颜色 红 尺码 37 1

(1 行受影响)

*/
-晴天 2011-09-26
  • 打赏
  • 举报
回复
create table tb(field nvarchar(10),value nvarchar(10),product_id int)
insert into tb select '颜色','红',1
insert into tb select '颜色','黄',1
insert into tb select '颜色','红',2
insert into tb select '尺码','35',1
insert into tb select '尺码','36',1
insert into tb select '尺码','37',1
insert into tb select '尺码','35',2
insert into tb select '尺码','36',2
go
select a.product_id,a.field,a.value,b.field,b.value
from tb a inner join tb b on a.product_id=b.product_id
where a.field='颜色' and a.value='红' and b.field='尺码' and b.value in('37','35')
/*
product_id field value field value
----------- ---------- ---------- ---------- ----------
1 颜色 红 尺码 35
1 颜色 红 尺码 37
2 颜色 红 尺码 35

(3 行受影响)
*/
go
drop table tb
ylmdy 2011-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]

尺码 37 1
不是ID是1么?
[/Quote]

不好意思,我写错了,是1没错。谢谢fredrickhu的提醒
-晴天 2011-09-26
  • 打赏
  • 举报
回复
select a.field,a.value,b.field,b.value 
from tb a inner join tb b on a.product_id=b.product_id
where a.field='颜色' and a.value='红' and b.field='尺码' and b.value in('37','35')
xuchaofeng 2011-09-26
  • 打赏
  • 举报
回复
楼主最好描述得不够清楚, 怎样的对应关系?
--小F-- 2011-09-26
  • 打赏
  • 举报
回复
尺码 37 1
不是ID是1么?
Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧