title: MySQL foreign key select matches
body: <p>I have a database with articles. All articles have keywords. The keywords are in a separate table and keywords and articles are connected with a relational table, thusly:</p>
  
  <p><strong>articles</strong></p>
  
  <pre><code>CREATE TABLE `articles` (
    `id` int(11) NOT NULL,
    `articleTitle` varchar(50) NOT NULL,
    `articleBody` text NOT NULL,
    `articlePermalink` varchar(50) NOT NULL,
    `articleAuthor` int(11) NOT NULL,
    `createdAt` datetime DEFAULT CURRENT_TIMESTAMP,
    `updatedAt` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p><strong>keywords</strong></p>
  
  <pre><code>CREATE TABLE `keywords` (
    `id` int(11) NOT NULL,
    `keyword` varchar(64) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p><strong>articlekeywords</strong></p>
  
  <pre><code>CREATE TABLE `articlekeywords` (
    `id` int(11) NOT NULL,
    `articleId` int(11) NOT NULL,
    `keywordId` int(11) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p>I want to select all articles that have some or all keywords in common with any given article using articles.id as the identifier. Preferably with a single SQL call.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I ended up doing something very similar to what <a href="https://stackoverflow.com/a/41323806/1986451">Gordon Linoff suggested below</a>.</p>
  
  <pre><code>SELECT DISTINCT a.articleTitle, a.articlePermalink
  FROM articles a
  JOIN articlekeywords ak ON a.id = ak.articleId
  JOIN articlekeywords ak2 ON ak.keywordId = ak2.keywordId AND ak2.articleId = $articleId AND not(a.id = $articleId)
  </code></pre>