title: Django raw SQL query trouble with format characters and string interpolation
body: <p>In my Django app, I need to generate a MySQL query like this:</p>
  
  <pre><code>SELECT * FROM player WHERE (myapp_player.sport_id = 4 AND (myapp_player.last_name LIKE 'smi%'))
  UNION
  SELECT * FROM player WHERE (myapp_player.sport_id = 4 AND (myapp_player.first_name LIKE 'smi%'));
  </code></pre>
  
  <p>I can't use Q objects to OR together the __istartswith filters because the query generated by the Django ORM does not use UNION and it runs at least 40 times slower than the UNION query above. For my application, this performance is unacceptable.</p>
  
  <p>So I'm trying stuff like this:</p>
  
  <pre><code>Player.objects.raw("SELECT * FROM myapp_player WHERE (sport_id = %%s AND (last_name LIKE '%%s%')) UNION SELECT * FROM sports_player WHERE (sport_id = %%s AND (first_name LIKE '%%s%'))", (sport.id, qword, sport.id, qword))
  </code></pre>
  
  <p>I apologize for the long one-liner, but I wanted to avoid using a triple-quoted string while trying to debug this type of issue.</p>
  
  <p>When I execute or repr this queryset object, I get exceptions like this:</p>
  
  <pre><code>*** ValueError: unsupported format character ''' (0x27) at index 133
  </code></pre>
  
  <p>That's a single-quote in single quotes, not a triple-quote. If I get rid of the single-quotes around the LIKE clauses, then I get a similar exception about the close-paren ) character that follows the LIKE clause. </p>
  
  <p>Apparently Django and MySQL disagree on the correct syntax for this query, but is there a syntax that will work for both?</p>
  
  <p>Finally, I'm not sure that my %%s syntax for string interpolation is correct, either. The Django docs suggest that I should be able to use the regular %s syntax in the arguments for raw(), but several online resources suggest using %%s or ? as the placeholder for string interpolation in raw SQL.</p>
  
  <p>My sincere thanks for just a little bit of clarity on this issue!</p>