Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL query - how to get a count of number of related tags
    primarykey
    data
    text
    <p>I am struggling figuring out a good way to query my required data. I am using mySQL and php. Currently, I am making multiple queries and using a bunch of for each loops, however, I still can't get my necessary output.</p> <p>This might be tough question, so I appreciate anyone who puts in some effort to figure it out! </p> <hr> <p>My current tables are as follows:</p> <pre><code>topics_keywords (t_k_id, topic_id, keyword_id) keywords (keyword_id, keyword) topics (topic_id, subject, etc) </code></pre> <p>table <code>topics_keyword</code> is going to have multiple keywords associated with the same topic_id.</p> <p>table <code>keywords</code> will only have define a single keyword for each keyword_id.</p> <p>If someone queries for a specific keyword(i.e. sports), I would like to return a list of all associated keywords that relate to the topic_id.</p> <p>In the below SQL, you'll see that topic 3 is associated with keyword_id's 3(sports) and 4(baseball). However, note that it is also associated with keyword_id 2(hello). </p> <p>I simply need a count of how many times 'sports' is associated with baseball. I say simply, but i can't figure out an easy way to do it.</p> <p>The final output, based on the data below, would need to look something like this:</p> <p>search term: 'baseball' final output:</p> <pre><code>count | keyword ---------------- sports | 2 hello | 1 </code></pre> <p>Heres some mySQL to help go through the data:</p> <pre><code>CREATE TABLE IF NOT EXISTS `keywords` ( `keyword_id` int(11) NOT NULL AUTO_INCREMENT, `keyword` varchar(64) NOT NULL, PRIMARY KEY (`keyword_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; -- -- Dumping data for table `keywords` -- INSERT INTO `keywords` (`keyword_id`, `keyword`) VALUES (1, 'thebump'), (2, 'hello'), (3, 'baseball'), (4, 'sports'), (5, 'manga'), (6, 'naruto'), (7, 'one piece'); -- -------------------------------------------------------- -- -- Table structure for table `topics_keywords` -- CREATE TABLE IF NOT EXISTS `topics_keywords` ( `t_k_id` int(11) NOT NULL AUTO_INCREMENT, `topics_id` int(11) NOT NULL, `keyword_id` int(11) NOT NULL, PRIMARY KEY (`t_k_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ; -- -- Dumping data for table `topics_keywords` -- INSERT INTO `topics_keywords` (`t_k_id`, `topics_id`, `keyword_id`) VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 3, 4), (5, 4, 3), (6, 5, 3), (7, 5, 4), (8, 6, 3), (9, 6, 4), (10, 3, 2); </code></pre> <p>Thanks very much!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload