title: MySQL 5.7: UNIQUE and exists
body: <p>I have 3 tables: usuaris, versions and usuari_versio for the relationship:</p>
  
  <pre><code>id | usuari_id | versio_id | updated_at | created_at
  </code></pre>
  
  <p>The following query, generated by laravel, is returning the user only if the first row of the relationship has versio_id = 2 when email has UNIQUE constraint. </p>
  
  <p>However, if I remove the UNIQUE constraint the query is returning the user for any versio_id in relationship (as desired).</p>
  
  <p>I have also seen that if instead of using email I use <code>usuaris</code>.<code>id</code>, which is PK, the same result with email with UNIQUE constraint is happening.</p>
  
  <pre><code>select * 
  from `usuaris`
  where 
      `usuaris`.`email` = 'mail@mail.com'
      and exists (
          select * 
          from `versions` 
          inner join `usuari_versio` on `versions`.`id` = `usuari_versio`.`versio_id` 
          where 
              `usuari_versio`.`usuari_id` = `usuaris`.`id` 
              and `versions`.`id` = 2
      ) 
  limit 1
  </code></pre>
  
  <p>I don't really understand why is happening this. But I need to get the user when the subquery is returning true and also when email (or id) is matching.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I tried changing the "=" of the subquery for a "like" (without %) and now is working as I expected:</p>
  
  <pre><code>...
  and `versions`.`id` LIKE 2
  ...
  </code></pre>
  
  <p>It is also working if I change the "=" for a "like" of email comparison.</p>
  
  <p>Why is happening that?</p>