Jump to content

[SOLVED] 'Intersect' style query


asmith3006

Recommended Posts

Hi,

 

I have a table used for indexing categories of products like:

Product | Category
--------|---------
ABC      | Plastic
ABC      | Red
ABC      | Tall
123      | Plastic
123      | Blue
123      | Tall

 

Now, how can I select all the tall & plastic products? This table could grow quite large so I want to keep this as efficient as possible.

 

I thought that some kind of intersect query would work, but mySQL doesn't have them apparently :(

 

One more complication to throw in is that I don't know how many categories there will be. I may need to search for one or five or.....

 

 

 

Any and all help much appreciated.

 

Thanks.

 

Andrew.

Link to comment
Share on other sites

Wont that simply give me all the products that are EITHER tall or plastic? I want products which are BOTH tall AND plastic.

 

e.g. if I were to search for a tall AND blue product I only want to find 123 but if I search for a tall AND plastic product I want ABC AND 123.

 

Thanks.

Link to comment
Share on other sites

I see

 

That's what I've come up with... probably there's better way though

 

SELECT product,  GROUP_CONCAT(category ORDER BY category) AS c FROM pc GROUP BY product HAVING c LIKE '%Blue%' AND c LIKE '%Tall%'

Link to comment
Share on other sites

You may want to read this thread.

 

SELECT pc.product
FROM pc as t1
CROSS JOIN pc as t2 USING ( id )
WHERE pc.category = 'plastic' and pc.category = 'tall'

 

Obviously, if you don't have an id field, you should use USING ( product, category ) -- and have a combined index.

 

Hope that helps.

Link to comment
Share on other sites

0 rows fetched...

 

I think you meant something like this:

 

SELECT product 
FROM pc AS t1 
CROSS JOIN pc AS t2 USING (product) 
WHERE t1.category = "Tall" AND t2.category = "Blue"

Yes, sorry, good catch -- it's really late in EST.

Link to comment
Share on other sites

Hi

 

Another option might be

 

SELECT a.product
FROM table a
JOIN table b ON a.product = b.product
WHERE a.category = 'Plastic'
AND b.category = 'Tall'

 

Would be easy to add more joins (and where clauses to go with them) if there are more required categories.

 

All the best

 

Keith

Link to comment
Share on other sites

You may want to read this thread.

 

SELECT pc.product
FROM pc as t1
CROSS JOIN pc as t2 USING ( id )
WHERE pc.category = 'plastic' and pc.category = 'tall'

 

Obviously, if you don't have an id field, you should use USING ( product, category ) -- and have a combined index.

 

Hope that helps.

Humm... you say 'obviously'.... I don't really understand multi-field keys. Can you elaborate a bit please?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.