title: Select the first and last word in a comma separated string SQL
body: <p>I'm trying to solve a seemingly easy task, but can't find the exact solution. </p>
  
  <p>I have a string with comma separated categories in MySQL, such as: Beauty, Cosmetics, Perfume</p>
  
  <p>I would like to get only the first and last word (category) from the string.  <strong>Beauty</strong> or <strong>Perfume</strong>.</p>
  
  <p>I have been trying numerous solutions like this: </p>
  
  <pre><code>SELECT RIGHT(categories, CHARINDEX(',',REVERSE(categories))-1) FROM data
  </code></pre>
  
  <p>but each of them doesn't work in my case. </p>
  
  <p>It doesn't matter how many words the string contain, I only need the first and last word of this string. </p>
  
  <p>In addition, I need to implement such values in another query such as:</p>
  
  <pre><code>SELECT * FROM data WHERE type = '$type' AND ^^^WHAT I NEED^^^ = '$first_category' AND item_type = '$item_type' AND slug = '$slug' 
  </code></pre>
  
  <p>It would be perfect if I could cope with both tasks in just one query.</p>