Jump to content

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
https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/
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.

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.

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

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?
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.