kc9ddi Posted December 2, 2006 Share Posted December 2, 2006 I'm developing a simple photo album project. I have one table that stores information about each photo (filename, format, size, etc.) and then a table of keywords. The keywords table looks like:[code]id INT primary key,keyword VARCHAR,photo_id INT[/code]Each keyword gets its own row in the table, even if multiple photos have the same keyword. Also, each photo can have multiple keywords that point to the same photo_id. I think this is a pretty standard setup.What I'm trying to do is something like SELECT * FROM photos WHERE keyword='keyword1' AND keyword='keyword2' -- that is, find all photos that have BOTH keyword1 and keyword2. I figured out a JOIN statement that does this for keyword1 OR keyword2, but am having trouble working one that does AND.Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/29248-1n-join-question/ Share on other sites More sharing options...
artacus Posted December 2, 2006 Share Posted December 2, 2006 Join the keywords table twice, once for each keyword. Just give the tables different aliases. Quote Link to comment https://forums.phpfreaks.com/topic/29248-1n-join-question/#findComment-134068 Share on other sites More sharing options...
kc9ddi Posted December 2, 2006 Author Share Posted December 2, 2006 I see - so, then, I'd need an additional join for each keyword I want to search? eg, 4 keywords = 4 joins? Quote Link to comment https://forums.phpfreaks.com/topic/29248-1n-join-question/#findComment-134074 Share on other sites More sharing options...
kc9ddi Posted December 2, 2006 Author Share Posted December 2, 2006 I hate to ask this, but I'm having trouble working out the syntax for the SQL - would you mind giving me an example? Quote Link to comment https://forums.phpfreaks.com/topic/29248-1n-join-question/#findComment-134086 Share on other sites More sharing options...
artacus Posted December 2, 2006 Share Posted December 2, 2006 [code]SELECT p.*FROM photos AS pJOIN keywords AS k1 ON p.photo_id = k1.photo_id JOIN keywords AS k2 ON p.photo_id = k2.photo_idWHERE k1.keyword LIKE 'keyword1' AND k2.keyword LIKE 'keyword2'[/code]or if you wanted to do it weighted where it could match either one or both of the keywords[code]SELECT p.*,CASE WHEN k1.id IS NOT NULL AND k2.id IS NOT NULL THEN 2 WHEN k1.id OR k2.id IS NOT NULL THEN 1 ELSE 0 END AS search_scoreFROM photos AS pLEFT JOIN keywords AS k1 ON p.photo_id = k1.photo_id AND k1.keyword LIKE 'keyword1'LEFT JOIN keywords AS k2 ON p.photo_id = k2.photo_id AND k2.keyword LIKE 'keyword2'GROUP BY p.photo_id HAVING search_score > 0 ORDER BY search_score DESC[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29248-1n-join-question/#findComment-134096 Share on other sites More sharing options...
fenway Posted December 3, 2006 Share Posted December 3, 2006 Fancy... however, depending on how expensive this is, you many simply want to get back all records with any keyword, and do the scoring yourself. Quote Link to comment https://forums.phpfreaks.com/topic/29248-1n-join-question/#findComment-134498 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.