Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Query help : join, plus AND
Old 12-12-2007, 11:13 AM Query help : join, plus AND
Average Talker

Posts: 29
Name: Jean
Trades: 0
Hi,

I have three tables :
- table item (id_item, title)
- table keyword (id_keyword, word)
- table item_keyword (id_item_keyword, id_item, id_keyword)

So, an item can be associated with 0 to n keywords, and a keyword with 0 to n items.
My purpose : finding the items who are associated with AT LEAST every keywords (id given).
Exemple : I'm giving as a parameter the ids 2,3,5. Item A is associated with keywords with ids 2,3,5,8 : it should be returned. Item B is associated with keywords ids 2,3,24,36 : it should not be returned.

If i make a simple join on id_item, and then an "IN" for the ids, it's an "OR" on the ids, i'd like an "AND".
Code:
SELECT DISTINCT it.id_item, it.title
FROM item it INNER JOIN item_keyword ik ON it.id_item = ik.id_item
WHERE ik.id_keyword IN (2,3,5)
Thanks
MarvinLeRouge is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-18-2007, 02:39 AM Re: Query help : join, plus AND
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
You wouldn't be able to accomplish this with one query since, as you pointed out, IN acts as an "OR" Operator and since your id_keywords are in different rows.

This may be a case for subqueries, but my experience with them is limited.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 12-18-2007, 03:24 PM Re: Query help : join, plus AND
Average Talker

Posts: 29
Name: Jean
Trades: 0
Well, the only way i've found for now is
Code:
SELECT id_item, title
FROM item WHERE
(id_item IN (SELECT id_item FROM item_motcle WHERE id_motcle = 2))
AND
(id_item IN (SELECT id_item FROM item_motcle WHERE id_motcle = 3))
AND
(id_item IN (SELECT id_item FROM item_motcle WHERE id_motcle = 5))
I also found a way to make it with inner join, but it's much less readable :
Code:
SELECT DISTINCT it.id_item, it.title
FROM item it INNER JOIN item_motcle a ON it.id_item = a.id_item
INNER JOIN item_motcle b ON a.id_item = b.id_item
INNER JOIN item_motcle c ON b.id_item = c.id_item
WHERE a.id_motcle = 2 AND b.id_motcle = 3 AND c.id_motcle = 5
MarvinLeRouge is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Query help : join, plus AND
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.33163 seconds with 12 queries