mySQL 专家 - 需要“相交”方面的帮助

发布于 2024-08-28 06:47:47 字数 620 浏览 7 评论 0原文

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见终念 2024-09-04 06:47:47

嗯,我不确定这是最好的方法,但它可以很容易地添加到您已经存在的查询中:

SELECT *
FROM products
WHERE p_show='Y'
    AND p_id IN (SELECT p_id
                 FROM prods_2cats AS PC
                 WHERE PC.cat_id = $cat1_id)
    AND p_id IN (SELECT p_id
                 FROM prods_2cats AS PC2
                 WHERE PC2.cat_id = $cat2_id)

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:

SELECT *
FROM products
WHERE p_show='Y'
    AND p_id IN (SELECT p_id
                 FROM prods_2cats AS PC
                 WHERE PC.cat_id = $cat1_id)
    AND p_id IN (SELECT p_id
                 FROM prods_2cats AS PC2
                 WHERE PC2.cat_id = $cat2_id)
画尸师 2024-09-04 06:47:47

应用集合论中交集的定义。 认为 x 在 X 中并且 x 在 Y 中。

因此,如果 X 与 Y 相交给出集合 Z,则对于 Z 中的每个 x,它都

SELECT * FROM products WHERE p_show='Y' AND p_id IN 
( SELECT p_id FROM prods_2cats AS PC WHERE PC.cat_id =X) AND p_id IN
( SELECT p_id FROM prods_2cats AS PC WHERE PC.cat_id =Y)

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

SELECT * FROM products WHERE p_show='Y' AND p_id IN 
( SELECT p_id FROM prods_2cats AS PC WHERE PC.cat_id =X) AND p_id IN
( SELECT p_id FROM prods_2cats AS PC WHERE PC.cat_id =Y)
淡莣 2024-09-04 06:47:47
SELECT *
FROM products
WHERE p_show='Y'
    AND p_id IN (SELECT p_id
                 FROM prods_2cats AS PC
                 WHERE PC.cat_id in ($cat1_id, $cat2_id ))

或者更好

SELECT p.*
FROM products p INNER JOIN prods_2cats AS PC on p.p_id = PC.p_id
WHERE p_show='Y' and C.cat_id in ($cat1_id, $cat2_id )

希望这有帮助

SELECT *
FROM products
WHERE p_show='Y'
    AND p_id IN (SELECT p_id
                 FROM prods_2cats AS PC
                 WHERE PC.cat_id in ($cat1_id, $cat2_id ))

OR even better

SELECT p.*
FROM products p INNER JOIN prods_2cats AS PC on p.p_id = PC.p_id
WHERE p_show='Y' and C.cat_id in ($cat1_id, $cat2_id )

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文