表1 产品表 product(id,title)
表2 分类表 category(id,title) 这个问题中可以不要用到该表
表2 关系表 product_category(pid,cid)
其它关系表是多对多的,既一个分类有多个产品,一个产品可以属于多个分类
现在要求分类ID包含 3,4,6 的产品(既某产品一定要有这3个分类)的SQL。。谢谢大家。。
表2 分类表 category(id,title) 这个问题中可以不要用到该表
表2 关系表 product_category(pid,cid)
其它关系表是多对多的,既一个分类有多个产品,一个产品可以属于多个分类
现在要求分类ID包含 3,4,6 的产品(既某产品一定要有这3个分类)的SQL。。谢谢大家。。
解决方案 »
- 求助:特殊字符的处理
- SQL 语句求助
- C#编程,单步调试的时候从Mysql读取的数据存到myReader里,过一会就没了。
- 请教一个UNION and ORDER BY报错问题
- 如何删除db中除了pk其他都一样的记录呢?
- SQL语句更新字段问题
- postgresql中文乱码的问题
- 如何写sql在mysql中,一个字段(int)自动加1,用create table回复马上给分.
- 怎么查询积分大于6的所有用户信息啊
- mysql load或者mysqlimpoort 数据,txt格式,如何跳过数据格式错误的行(字段超长或者字段格式少或者多))?
- 关于 MYSQL存储过程
- 按以下查询操作,为什么俩个表记录数不一致呢
From product_category a,product_category b,product_category c
Where a.pid=b.pid and b.pid=c.pid
and a.cid=3 And b.cid=4 And c.cid=6
from product_category
where cid IN (3,4,6)
Group by pid
Having count(*)=3
create table newtb select * from oldtb
SELECT * FROM `product` as p WHERE
exists (select 1 from product_category where pid=p.id and cid=3)
and exists (select 1 from product_category where pid=p.id and cid=4)
and exists (select 1 from product_category where pid=p.id and cid=6)
and exists 虽然看上去这样的SQL很长,但我觉得product_category中的cid做了索引,且此SQL中的where是and和exists 这二个关键字执行效率都是很高的,不知道大家意下如何。
如果大家觉得还有好的方法,请告之,非常谢谢