title: MySQL JOIN & Split String
body: <p>I want to JOIN two table. Have no problem with that. I have problem with quite different thing.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>SELECT * FROM `table1`
  JOIN `table2` ON `table1.`field`=`table2`.`field`
  ...
  </code></pre>
  
  <p>The main issue is that <code>table1.field</code> is a string, comma-separated. Is there any good and fast way to split it?</p>
  
  <p><strong>Update</strong><br>
  I found a function by Federico Cagnelutti</p>
  
  <pre><code>CREATE FUNCTION SPLIT_STR(
    x VARCHAR(255),
    delim VARCHAR(12),
    pos INT
  )
  RETURNS VARCHAR(255)
  RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
         LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
         delim, '');
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;
  </code></pre>
  
  <p>Quite useful but it requires position integer as third parameter. Anyway, I <strong>do not</strong> know the amount of comma-separated words. Bascially, it's up to 20 words, but I don't know how many.</p>
  
  <p><strong>Update 2</strong>
  To clarify my question. Here's what I like to have (I know the following query is incorrect):</p>
  
  <pre><code>SELECT * FROM `table1`
  JOIN `table2` ON `table2`.`id` IN (`table1`.`field`)
  </code></pre>
  
  <p><strong>Update 3</strong><br>
  Example strings: <code>table1.field</code> = '202, 185, 505', <code>table2.field</code> = 202</p>