title: TYPO3 get pages by sys_categor(y/ies)
body: <p>I need your help, I do not see the forest for the trees.
  Consider the following scenario:</p>
  
  <p>A TYPO3 - system with, say, 2 pages. (About us &amp; Services)
  Furthermore, I have 3 System Categories (Cat 1, Cat 2 Cat 3)</p>
  
  <p>The page About us, the categories "Cat 1" &amp; "Cat 2".
  The page Services has the categories "Cat 2" &amp; "Cat 3".</p>
  
  <p>Now I need a SELECT query in typoscript from which I get only the page which "Cat 3" and "Cat 2 (In this case it would be the page Services).</p>
  
  <p>But let's start with simple SQL and not with a Typoscript query.</p>
  
  <p>The following 3 tables exist in TYPO3 for this situation:</p>
  
  <p>"pages", "sys_category", "sys_category_record_mm"</p>
  
  <pre><code>pages - table:
  +-----+----------+------------+
  | uid |  title   | categories |
  +-----+----------+------------+
  |   3 | About us |          2 |
  |   4 | Services |          2 |
  +-----+----------+------------+
  
  
  sys_category - table:
  +-----+-------+
  | uid | title |
  +-----+-------+
  |   1 | Cat 1 |
  |   2 | Cat 2 |
  |   3 | Cat 3 |
  +-----+-------+
  
  
  sys_category_record_mm - table:
  +-----------+-------------+------------+
  | uid_local | uid_foreign | tablenames |
  +-----------+-------------+------------+
  |         2 |           4 | pages      |
  |         2 |           3 | pages      |
  |         1 |           4 | pages      |
  |         3 |           3 | pages      |
  +-----------+-------------+------------+
  </code></pre>
  
  <p>Now i have a simple sql query which outputs all pages who got a category, like this:</p>
  
  <pre><code>SELECT DISTINCT title FROM pages
  JOIN sys_category_record_mm ON sys_category_record_mm.uid_foreign = pages.uid
  WHERE sys_category_record_mm.tablenames = 'pages'
  </code></pre>
  
  <p>This will get me following output:</p>
  
  <pre><code>+----------+
  |  title   |
  +----------+
  | Services |
  | About us |
  +----------+
  </code></pre>
  
  <p>But now I want to have only the page "Services" which has category "Cat 2" and category "Cat 3".</p>
  
  <p>How I have to modify my query to get only the page with both categories.</p>
  
  <p>I tried something like that but i know that this can't work, because I will still get both pages because of the OR condition.</p>
  
  <pre><code>SELECT DISTINCT title FROM pages
  JOIN sys_category_record_mm ON sys_category_record_mm.uid_foreign = pages.uid
  WHERE sys_category_record_mm.tablenames = 'pages' AND (sys_category_record_mm.uid_local = 2 OR sys_category_record_mm.uid_local = 3)
  </code></pre>
  
  <p>Thanks in advance!!</p>
  
  <p>I'm thankful for every tip.</p>