mySQL 专家 - 需要“相交”方面的帮助
我知道 mySQL 5.x 不支持 INTERSECT,但这似乎正是我所需要的。
表 A:产品 (p_id)
表 B:Prod_cats (cat_id) - 类别信息(名称、描述等)
表 C:prod_2cats (p_id、cat_id) - 多对多
prod_2cats 保存已被删除的多个(1 个或多个)类别分配给产品 (A)。
进行查询/过滤器查找(用户交互)并且需要能够跨多个类别选择满足所有条件的产品。
前任: - 80 种产品被分配到 X 类 - 50 个产品被分配到 Y 类 - 但只有 10 个产品(相交)分配给 cat X 和 cat Y
此 sql 适用于一种类别:
SELECT * FROM products WHERE p_show='Y' AND p_id IN ( SELECT p_id FROM prods_2cats
AS PC WHERE PC.cat_id =" . $cat_id ."
<-$cat_id 是从查询表单传递的经过清理的 var ,
我似乎找不到说“给我 cat A 和 cat B 的交集”并得到的方法。返回子集(10 条记录,来自我的示例)
救命!
I know that mySQL 5.x does not support INTERSECT, but that seems to be what I need.
Table A: Products (p_id)
Table B: Prod_cats (cat_id) - category info (name, description, etc)
Table C: prod_2cats (p_id, cat_id) - many to many
prod_2cats holds the many (1 or more) categories that have been assigned to Products (A).
Doing a query/filter lookup, (user interactive) and need to be able to select across multiple categories the products that meet ALL the criteria.
Ex:
- 80 products assigned to Category X
- 50 products assigned to Category Y
- but only 10 products (intersect) are assigned to BOTH cat X AND cat Y
This sql works for one category:
SELECT * FROM products WHERE p_show='Y' AND p_id IN ( SELECT p_id FROM prods_2cats
AS PC WHERE PC.cat_id =" . $cat_id ."
<-$cat_id is sanitized var passed from query form .
I can't seem to find the means to say ' give me the intersect of cat A and cat B' and get back the subset (10 records, from my example)
Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,我不确定这是最好的方法,但它可以很容易地添加到您已经存在的查询中:
Hmm, I'm not sure that it's the best way to do it, but it can be added on to your already-existing query pretty easily:
应用集合论中交集的定义。 认为 x 在 X 中并且 x 在 Y 中。
因此,如果 X 与 Y 相交给出集合 Z,则对于 Z 中的每个 x,它都
Apply the definition of intersection from set theory. So if X intersect Y gives set Z, then for each x in Z it holds that x is in X and x is in Y.
Something like
或者更好
希望这有帮助
OR even better
Hope this helps