sql查询 同一个字段 同时满足多个条件

zhoufeng81811 2010-09-27 01:10:11
如表: id 商品id 商品属性 属性内容
id productId attr content
1 1 颜色 红色
2 1 长度 10厘米
3 1 宽度 20厘米
4 2 颜色 红色
4 2 长度 20厘米


我就想从这个表中找出 颜色为红色,长度为10厘米 的商品的id 结果应该是 1
请问各位查询语句怎么写呢? 在线等!
...全文
4660 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
authoryu 2011-12-10
  • 打赏
  • 举报
回复
找到了 哈哈
zhoufeng81811 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 acmain_chm 的回复:]

如果你能确保 ( 商品id , 商品属性)唯一性。则可以

SQL code
select productId
from 如表
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
or (attr='宽度' and content='20厘米')
group by productId
having cou……
[/Quote]

这个很好 感谢。
ACMAIN_CHM 2010-09-27
  • 打赏
  • 举报
回复
如果你能确保 ( 商品id , 商品属性)唯一性。则可以

select productId
from 如表
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
or (attr='宽度' and content='20厘米')
group by productId
having count(*)=3
ACMAIN_CHM 2010-09-27
  • 打赏
  • 举报
回复
如果 条件再加上 宽度为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厘米')



ACMAIN_CHM 2010-09-27
  • 打赏
  • 举报
回复
[Quote]这个确实可以完成提出的功能,如果 条件再加上 宽度为20厘米,那估计就不行了,实际的需求是条件的数量不定[/Quote]
一次性建议把问题说完整,否则经常是这样,对于问题的方案出来了,结果问题本身就变了。这样反而是浪费包括提问者本身在内的所有人的时间和精力。


问题说明越详细,回答也会越准确!参见如何提问。(提问的智慧
rucypli 2010-09-27
  • 打赏
  • 举报
回复
select productId
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (count(*))>1
zhoufeng81811 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 rucypli 的回复:]

select productId
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (distinct attr)>1
[/Quote]

这个还是不对 我需要的是查询出 即满足颜色为红色 并且 长度为10厘米的商品id
rucypli 2010-09-27
  • 打赏
  • 举报
回复
select productId
from tb
where (attr='颜色' and content='红色')
or (attr='长度' and content='10厘米')
group by productId
having (distinct attr)>1
zhoufeng81811 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rucypli 的回复:]

select productId
from tb
where attr='颜色' and content='红色'
and attr='长度' and content='10厘米'
group by productId
having (distinct attr)>1
[/Quote]

这个语句执行的也是空
rucypli 2010-09-27
  • 打赏
  • 举报
回复
select productId
from tb
where attr='颜色' and content='红色'
and attr='长度' and content='10厘米'
group by productId
having (distinct attr)>1
zhoufeng81811 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 acmain_chm 的回复:]

SQL code
select productId
from 如表 a , 如表 b
where a.productId=b.productId
and a.attr='颜色' and a.content='红色'
and b.attr='长度' and b.content='10厘米'
[/Quote]

这个确实可以完成提出的功能,如果 条件再加上 宽度为20厘米,那估计就不行了,实际的需求是条件的数量不定
ACMAIN_CHM 2010-09-27
  • 打赏
  • 举报
回复
select productId
from 如表 a , 如表 b
where a.productId=b.productId
and a.attr='颜色' and a.content='红色'
and b.attr='长度' and b.content='10厘米'
zhoufeng81811 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 rucypli 的回复:]

select productId
from attr='红色' and attr='10厘米'
group by productId
[/Quote]

你这个返回的是空吧
rucypli 2010-09-27
  • 打赏
  • 举报
回复
select productId
from attr='红色' and attr='10厘米'
group by productId

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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