id productId attr content
1 1 颜色 红色
2 1 长度 10厘米
3 1 宽度 20厘米
4 2 颜色 红色
4 2 长度 20厘米
我就想从这个表中找出 颜色为红色,长度为10厘米 的商品的id 结果应该是 1
请问各位查询语句怎么写呢? 在线等!
14 个解决方案
#1
select productId
from attr='红色' and attr='10厘米'
group by productId
from attr='红色' and attr='10厘米'
group by productId
#2
你这个返回的是空吧
#3
select productId
from 如表 a , 如表 b
where a.productId=b.productId
and a.attr='颜色' and a.content='红色'
and b.attr='长度' and b.content='10厘米'
#4
这个确实可以完成提出的功能,如果 条件再加上 宽度为20厘米,那估计就不行了,实际的需求是条件的数量不定
#5
select productId
from tb
where attr='颜色' and content='红色'
and attr='长度' and content='10厘米'
group by productId
having (distinct attr)>1
from tb
where attr='颜色' and content='红色'
and attr='长度' and content='10厘米'
group by productId
having (distinct attr)>1
#6
这个语句执行的也是空
#7
select productId
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (distinct attr)>1
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (distinct attr)>1
#8
这个还是不对 我需要的是查询出 即满足颜色为红色 并且 长度为10厘米的商品id
#9
select productId
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (count(*))>1
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (count(*))>1
#10
一次性建议把问题说完整,否则经常是这样,对于问题的方案出来了,结果问题本身就变了。这样反而是浪费包括提问者本身在内的所有人的时间和精力。
问题说明越详细,回答也会越准确!参见如何提问。( 提问的智慧)
#11
如果 条件再加上 宽度为20厘米,那估计就不行了,实际的需求是条件的数量不定
select distcint productId
from 如表 a
where exists (select 1 from 如表 where productId=a.productId and attr='颜色' and content='红色')
and exists (select 1 from 如表 where productId=a.productId and attr='长度' and content='10厘米')
and exists (select 1 from 如表 where productId=a.productId and attr='宽度' and content='20厘米')
#12
如果你能确保 ( 商品id , 商品属性)唯一性。则可以
select productId
from 如表
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
or (attr='宽度' and content='20厘米')
group by productId
having count(*)=3
#13
这个很好 感谢。
#14
找到了 哈哈
#1
select productId
from attr='红色' and attr='10厘米'
group by productId
from attr='红色' and attr='10厘米'
group by productId
#2
你这个返回的是空吧
#3
select productId
from 如表 a , 如表 b
where a.productId=b.productId
and a.attr='颜色' and a.content='红色'
and b.attr='长度' and b.content='10厘米'
#4
这个确实可以完成提出的功能,如果 条件再加上 宽度为20厘米,那估计就不行了,实际的需求是条件的数量不定
#5
select productId
from tb
where attr='颜色' and content='红色'
and attr='长度' and content='10厘米'
group by productId
having (distinct attr)>1
from tb
where attr='颜色' and content='红色'
and attr='长度' and content='10厘米'
group by productId
having (distinct attr)>1
#6
这个语句执行的也是空
#7
select productId
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (distinct attr)>1
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (distinct attr)>1
#8
这个还是不对 我需要的是查询出 即满足颜色为红色 并且 长度为10厘米的商品id
#9
select productId
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (count(*))>1
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (count(*))>1
#10
一次性建议把问题说完整,否则经常是这样,对于问题的方案出来了,结果问题本身就变了。这样反而是浪费包括提问者本身在内的所有人的时间和精力。
问题说明越详细,回答也会越准确!参见如何提问。( 提问的智慧)
#11
如果 条件再加上 宽度为20厘米,那估计就不行了,实际的需求是条件的数量不定
select distcint productId
from 如表 a
where exists (select 1 from 如表 where productId=a.productId and attr='颜色' and content='红色')
and exists (select 1 from 如表 where productId=a.productId and attr='长度' and content='10厘米')
and exists (select 1 from 如表 where productId=a.productId and attr='宽度' and content='20厘米')
#12
如果你能确保 ( 商品id , 商品属性)唯一性。则可以
select productId
from 如表
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
or (attr='宽度' and content='20厘米')
group by productId
having count(*)=3
#13
这个很好 感谢。
#14
找到了 哈哈