Im finding it very hard to learn how to build more complicated queries in mySQL.
Hopefully, through this question, I can gain some more insight as to how to develop multi-level queries inside a single query.
I have a tagging system.
tbl1: ktbridge
ktbridge_id
threads_id
keywords_id
tbl2: threads
threads_id
subject
etc
tbl3: keywords
keywords_id
keyword
related_id
Above is my current set up.
For data, I have:
INSERT INTO `ktbridge` (`ktbridge_id`, `threads_id`, `keywords_id`) VALUES
(1, 1, 9);
INSERT INTO `keywords` (`keyword_id`, `keyword`, `related_id`) VALUES
(5, 'Sports', 0),
(6, 'Baseball', 5),
(7, 'football', 5),
(8, 'Curling', 5),
(9, 'Pitchers', 6),
(10, 'Catchers', 6),
INSERT INTO `threads` (`threads_id`, `subject`, `body`, `users_id`, `timestamp`, `replystamp`) VALUES
(1, 'Sports - Baseball - Pitchers - Subject', 'Sports - Baseball - Pitchers - Body', 1, '2013-01-09 14:09:40', '0000-00-00 00:00:00');
The goal here, is to return the threads that are related to the keyword_id that is provided.
The tough part is that if “baseball” is selected, it returns baseball threads. However, if “sports” is selected, it returns all threads below it, based on the “related_id” in the keywords table. So it would return all threads based on baseball, football, and curling.
Is this a possibility? Or will it require multiple queries? Thanks in advance!
It does require multiple queries, but you can nest them and select from within the inner select. I think the following should work (or at least get you started). Note that this is MySQL, but should be portable to other RDBM systems:
MySQL doc reference for “IN”