I need to create a query which does the following:
Given term_data.name (in vocabulary 1), use term_data.tid to retrieve term_node.nid, then use term_node.nid to get the tid of all other terms in term_node.nid who are attached to the same term_node.nid, then use all of the retrieved term_node.tids to get all of the term_data.names who are in vocabulary 2.
For example:
I have a vocabulary named Regions(vid=1), and a Vocabulary named Genres(vid=2). I classify my content_type_bookstore nodes by Region and Genre. So Bob's Used Books is in Burnaby (term_data.name=burnaby, term_data.tid=10, term_data.vid=1) and is genre Science Fiction (term_data.name=scifi, term_data.tid=20, term_data.vid=2)
I have a vocabulary named Regions and a vocabulary named Genres. I have a CCK content type named Bookstores. Bookstores are categorized by Regions and Genres. I need to query the database by giving it the Region, and have it return a list of all the Genres for all bookstores in that Region. I don't need to list the Bookstores along with the Genres. I've gotten this far:
SELECT DISTINCT term_data.name FROM (term_data INNER JOIN term_node ON term_data.tid = term_node.tid) INNER JOIN content_type_restaurant ON content_type_restaurant.nid = term_node.nid WHERE term_data.vid = 2
(term_data.vid=2 is "Genres")