title: Use LIMIT to paginate results in MySQL query
body: <p>I want to fetch my results a 'page' at a time; I want the page number to be a parameter (in a JDBC prepared statement). Consider the following snippet</p>
  
  <pre><code>SELECT * FROM thread t ORDER BY t.id LIMIT ((? - 1) * 20), 20
  </code></pre>
  
  <p>So ideally, this would result, for page 1, to <code>LIMIT 0, 20</code>.</p>
  
  <p>When I test</p>
  
  <pre><code>SELECT * FROM thread t ORDER BY t.id LIMIT ((1 - 1) * 20), 20
  </code></pre>
  
  <p>I am told I have a syntax error. I don't see what it could be, though - it's just some simple math. All it tells me is</p>
  
  <blockquote>
    <p>ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near '((1 -
     1) * 20), 20' at line 1</p>
  </blockquote>
  
  <p>What am I doing wrong with my <code>LIMIT</code> clause, and how can I fix it?</p>