title: How to Create MySQL Query to Find Related Posts from Multiple Tables?
body: <p>This is a complicated situation (for me) that I'm hopeful someone on here can help me with.  I've done plenty of searching for a solution and have not been able to locate one.  This is essentially my situation...  (I've trimmed it down because if someone can help me to create this query I can take it from there.)</p>
  
  <p>TABLE articles (article_id, article_title)</p>
  
  <p>TABLE articles_tags (row_id, article_id, tag_id)</p>
  
  <p>TABLE article_categories (row_id, article_id, category_id)</p>
  
  <p>All of the tables have article_id in common.  I know what all of the tag_id and category_id rows are.  What I want to do is return a list of all the articles that article_tags and article_categories MAY have in common, ordered by the number of common entries.</p>
  
  <p>For example:</p>
  
  <p>article1 - tags: tag1, tag2, tag3 - categories: cat1, cat2</p>
  
  <p>article2 - tags: tag2 - categories: cat1, cat2</p>
  
  <p>article3 - tags: tag1, tag3 - categories: cat1</p>
  
  <p>So if my article had "tag1" and "cat1 and cat2" it should return the articles in this order:</p>
  
  <p>article1 (tag1, cat1 and cat2 in common)</p>
  
  <p>article3 (tag1, cat1 in common)</p>
  
  <p>article2 (cat1 in common)</p>
  
  <p>Any help would genuinely be appreciated!  Thank you!</p>