sql 商品属性值查询问题

Doure Ma 2009-11-19 10:45:41
如:
表商品属性值表:
ProductAttributeValue

编号 ID
商品编号 ProductID
属性编号 AttributeID
属性值编号 AttributeValueID


请问:

如何查询 attributeValueID = 1 和 attributeValueID= 2 和 attributeValueID= 3

如何查询效率最高?
...全文
307 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
guodongbb 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fredrickhu 的回复:]
就你自己的比较快了
用连接是效率比较高的

实在是不快的话加索引了
[/Quote]
支持!
--小F-- 2009-11-19
  • 打赏
  • 举报
回复
就你自己的比较快了
用连接是效率比较高的

实在是不快的话加索引了
syw_java 2009-11-19
  • 打赏
  • 举报
回复
速度问题?应该没什么大的影响
Doure Ma 2009-11-19
  • 打赏
  • 举报
回复
谢谢回复。
这个方法可行,但是,distcint group in这样效率是很低的。
我想了个方法:
Select * From MaLiang_ProductAttributeValue AS A
Inner Join MaLiang_ProductAttributeValue As B On A.ProductID = B.ProductID
Inner Join MaLiang_ProductAttributeValue As C On A.ProductID = C.ProductID
Where
A.AttributeValueID = 1
And B.AttributeValueID = 100
And C.AttributeValueID = 200

我这个方法速度不快。
请问:还有更好的方法吗?
不胜感激。
Doure Ma 2009-11-19
  • 打赏
  • 举报
回复
谢谢回复。
这个方法可行,但是,distcint group in这样效率是很底的。
我下了个方法:
Select * From MaLiang_ProductAttributeValue AS A
Inner Join MaLiang_ProductAttributeValue As B On A.ProductID = B.ProductID
Inner Join MaLiang_ProductAttributeValue As C On A.ProductID = C.ProductID
Where
A.AttributeValueID = 1
And B.AttributeValueID = 100
And C.AttributeValueID = 200

这个方法速度不快。
请问:还有更好的方法吗?
不胜感激。
dawugui 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 maliangdonet 的回复:]
谢谢回复。

用Or 不对,要查的是商品属性值=1,并且=2,并且=3;
商品和商品属性值是一对多的关系。
用between 可以,但是如果,attributeValueID = 1, attributeValueID = 100,attributeValueID = 200那?
条件是不连续的?
[/Quote]
假设查的是id
select * from ProductAttributeValue where id in 
(
select id from
(
select distcint ID from ProductAttributeValue where attributeValueID = 1
union all
select distcint ID from ProductAttributeValue where attributeValueID = 2
union all
select distcint ID from ProductAttributeValue where attributeValueID = 3
) t group by id having count(1) = 3
)


select * from ProductAttributeValue where id in 
(
select id from
(
select distcint ID from ProductAttributeValue where attributeValueID = 1
union all
select distcint ID from ProductAttributeValue where attributeValueID = 100
union all
select distcint ID from ProductAttributeValue where attributeValueID = 200
) t group by id having count(1) = 3
)
Doure Ma 2009-11-19
  • 打赏
  • 举报
回复
谢谢回复。

用Or 不对,要查的是商品属性值=1,并且=2,并且=3;
商品和商品属性值是一对多的关系。
用between 可以,但是如果,attributeValueID = 1, attributeValueID = 100,attributeValueID = 200那?
条件是不连续的?
dawugui 2009-11-19
  • 打赏
  • 举报
回复
select * from ProductAttributeValue where attributeValueID = 1 or attributeValueID= 2 or attributeValueID = 3

select * from ProductAttributeValue where attributeValueID between 1 and 3
guguda2008 2009-11-19
  • 打赏
  • 举报
回复
加索引
dawugui 2009-11-19
  • 打赏
  • 举报
回复
select * from ProductAttributeValue where attributeValueID = 1 or attributeValueID= 2 or attributeValueID= 3

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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