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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.