asmith3006 Posted March 1, 2009 Share Posted March 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/ Share on other sites More sharing options...
Mchl Posted March 1, 2009 Share Posted March 1, 2009 SELECT * FROM table WHERE Category = 'Plastic' OR Category = 'Tall'; or SELECT * FROM table WHERE Category IN ('Plastic','Tall'); Also create an index on Category column. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-773966 Share on other sites More sharing options...
asmith3006 Posted March 3, 2009 Author Share Posted March 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-775397 Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 AND is more restrictive than OR as it requires more conditions to be met. OR will give you the larger recordset based on the same condition. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-775431 Share on other sites More sharing options...
Mchl Posted March 3, 2009 Share Posted March 3, 2009 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%' Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-775438 Share on other sites More sharing options...
asmith3006 Posted March 4, 2009 Author Share Posted March 4, 2009 Is that more efficient than doing SELECT product FROM product_table WHERE category = 'tall' IN (SELECT product FROM product_table WHERE category = 'plastic');? Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776168 Share on other sites More sharing options...
Mchl Posted March 4, 2009 Share Posted March 4, 2009 I'm not sure. But if you say you need to search on multiple categories, nested subqueries might get really slow... Not to mention tricky to create. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776182 Share on other sites More sharing options...
fenway Posted March 4, 2009 Share Posted March 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776206 Share on other sites More sharing options...
Mchl Posted March 4, 2009 Share Posted March 4, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776212 Share on other sites More sharing options...
fenway Posted March 4, 2009 Share Posted March 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776217 Share on other sites More sharing options...
Mchl Posted March 4, 2009 Share Posted March 4, 2009 Still I'm ashamed that I didn't learn from the topic mentioned... Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776240 Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776262 Share on other sites More sharing options...
Mchl Posted March 4, 2009 Share Posted March 4, 2009 That's exactly same query. JOIN, CROSS JOIN and INNER JOIN are synonymic for MySQL Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776275 Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 Hi Sorry, I have had too many varieties of SQL inflicted on me over time! All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776291 Share on other sites More sharing options...
Mchl Posted March 4, 2009 Share Posted March 4, 2009 We all wish for standard implementations Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776292 Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 Hi Worst is when you are using different standards at the same time on different projects. Even M$ cannot manage to have an SQL standard between sql server and access. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776300 Share on other sites More sharing options...
asmith3006 Posted March 4, 2009 Author Share Posted March 4, 2009 Oooohh I like the look of the JOIN one. I will try that. Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776561 Share on other sites More sharing options...
asmith3006 Posted March 4, 2009 Author Share Posted March 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776563 Share on other sites More sharing options...
fenway Posted March 4, 2009 Share Posted March 4, 2009 Sorry, Mchl caught an earlier mistake... you only need a key on the column you're joining. Quote Link to comment https://forums.phpfreaks.com/topic/147453-solved-intersect-style-query/#findComment-776672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.