MySQL Forums
Forum List  »  Newbie

JOIN from 3 tables - not possible to do it with subquerys
Posted by: Tobias Kazmierczak
Date: February 01, 2007 12:30AM

I have 3 tables looking like this:

images:
- id CHAR(9)
- name VARCHAR(60)
- descr
- size
- enabled ENUM(yes, no)
- premium ENUM(yes, no)

keywords:
- id INT(10)
- keyword_en VARCHAR(60)

imgkey:
- id_img CHAR(9)
- id_key INT(10)
- relevance ENUM(1,2,3,4,5)

the imgkey is the relationship table, because n keywords can belong to m images.

Now i want to query the database and images with a specific keyword ordered by relevance:

SELECT i.id
FROM images i
JOIN imgkey ik
JOIN keywords k
WHERE i.id = ik.id_img
AND k.id = ik.id_key
AND k.keyword_en = "Jesus"
ORDER By ik.relevance DESC

It works pretty good, as long as i only search for one keyword. but if i add another keyword with an AND i´ll get no results. it´s clear, because on joined col can´t have both keywords. but i can´t use an OR, because i want get the right results.

i also thought about a subquery, but it doesn´t work, because i need to sort the images by relevance.

Do you have any ideas! thanks a lot for your help!

Options: ReplyQuote


Subject
Written By
Posted
JOIN from 3 tables - not possible to do it with subquerys
February 01, 2007 12:30AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.