Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This behaviour is by design, not only in MySQL.</p> <p>You can work around it in <em>comparisons</em> by using <code>BINARY</code>:</p> <pre><code>mysql&gt; select version(), 'a' = 'a ', BINARY 'a' = BINARY 'a '; +-------------+------------+--------------------------+ | version() | 'a' = 'a ' | BINARY 'a' = BINARY 'a ' | +-------------+------------+--------------------------+ | 5.5.25a-log | 1 | 0 | +-------------+------------+--------------------------+ 1 row in set (0.00 sec) </code></pre> <p>but not much more. This will help you with <code>SELECT</code>s if whitespaces appear e.g. in user input to a search; but if you want to actually input whitespace-trailed information, it will be a problem (you can't have an index with both 'a' and 'a ').</p> <p>See also</p> <p><a href="https://stackoverflow.com/questions/3668996/trailing-whitespace-in-varchar-needs-to-be-considered-in-comparison">Trailing whitespace in varchar needs to be considered in comparison</a></p> <p>You could conceivably <em>reverse</em> the strings in that column, and reverse them back when displaying them. Of course this will wreck any ordering based on that column, but if you only test equality or substring existence, it just might work. <em>Leading</em> spaces do count.</p> <p>For equality searches you might also store the base64 encoding of the string, which ought to maintain the lexicographical order (i.e., the order between a and b ought to be maintained between base64(a) and base64(b)). Or you might append a terminator on the string ("\n" could do well and not appear in searches).</p> <p>Finally, but it's risky because humans can't tell the difference, you could replace spaces with the UTF8 char(49824):</p> <pre><code>mysql&gt; select concat ('\'a', char(49824),'\'') AS tricked, concat ('\'a', ' ' ,'\'') as honest, concat ('\'a', char(49824),'\'') = concat ('\'a', ' ' ,'\'') as equals; +---------+--------+--------+ | tricked | honest | equals | +---------+--------+--------+ | 'a ' | 'a ' | 0 | +---------+--------+--------+ 1 row in set (0.00 sec) </code></pre> <p>The rows <em>seem</em> to be equal, but they are not. Note that in HTML the space is a space, and 49824 is <code>&amp;nbsp;</code> (nonbreaking space). This affects functions that convert to and fro HTML, and the nbsp being actually an UTF8 codepoint means that <em>honest</em> string is two bytes, but length of <em>tricked</em> string is actually <em>three</em>.</p> <p>Finally you can declare the column <code>VARBINARY</code> instead of <code>VARCHAR</code>, thus completely hiding what's happening. It looks like the easiest solution, but I fear it might bite you some weeks or months down the line.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload