mySQL experts - need help with 'intersect'
Posted
by MTCreations
on Stack Overflow
See other posts from Stack Overflow
or by MTCreations
Published on 2010-04-05T20:46:28Z
Indexed on
2010/04/05
21:03 UTC
Read the original article
Hit count: 419
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!
© Stack Overflow or respective owner