title: MySQL prepared statements with order by clause
body: <p>I am using prepared statements in MySQL with 'order by' conditional clause. Using '?' and variables don't work with such things like ordering so I've decided to make it in other way. I set if conditions, but it's much more code. Maybe there is other option to reduce code and just changing 'order by' arguments? </p>
  
  <pre><code>IF sorting_column_index = 1 and sorting_column_mode = 0
          THEN PREPARE STMT FROM 'SELECT a.oid as \'oid\', 
          ...
  
          FROM table as a
  
          ...
          order by numero_annee desc LIMIT ?, ?';
          EXECUTE STMT USING @skip, @ROWS;
  END IF;
  
  IF sorting_column_index = 2 and sorting_column_mode = 1
          THEN PREPARE STMT FROM 'SELECT a.oid as \'oid\', 
          ...
  
          FROM table as a
  
          ...
          order by numero_ordre asc LIMIT ?, ?';
          EXECUTE STMT USING @skip, @ROWS;
  END IF;
  ...
  </code></pre>