title: Remove duplicate form select result
body: <h1>#edited</h1>
  
  <p>Ok. I will delete all role permissions and add them again.</p>
  
  <p>Is it possible to combine all 4 queries into one?</p>
  
  <hr>
  
  <p>I am creating a permission system.</p>
  
  <p>Assumptions:</p>
  
  <ul>
  <li><p>Each user can have more than one role</p></li>
  <li><p>Each role can have more than one permission</p></li>
  <li><p>Permissions can also be assigned directly to the user (they have a higher priority than permissions for roles)</p></li>
  </ul>
  
  <p>The priority of permissions is:</p>
  
  <ul>
  <li><p>role permission</p></li>
  <li><p>denial of role</p></li>
  <li><p>user permission</p></li>
  <li><p>denied to user</p></li>
  </ul>
  
  <p>Denying the user has the highest priority</p>
  
  <hr>
  
  <p>The matter in PHP is quite simple:</p>
  
  <ul>
  <li><p>I create an array with all permissions</p></li>
  <li><p>I am getting permissions for a role (order by access)</p></li>
  <li><p>I assign access, if it's denied, I overwrite access with denied</p></li>
  <li><p>I do the same for user permissions</p></li>
  <li><p>I assign access, if it's denied, I overwrite access with denied</p></li>
  </ul>
  
  <p>This way I have the whole array with permissions for a specific user, e.g. $ user['permission']['delete_post'] // output: false || true</p>
  
  <p>I need to do permission inspection now. This means which user has access to e.g. 'delete_post'</p>
  
  <p>I have this database scructure:
  <img src="https://scontent-waw1-1.xx.fbcdn.net/v/t1.0-9/70590333_457218828340804_3608592236033343488_n.jpg?_nc_cat=102&amp;_nc_oc=AQnGibnOo1is06WBQttvttJhQLXpd9A7yj3D_CQjVyet2V3qvQWm_e9cZqO4MBml2XM&amp;_nc_ht=scontent-waw1-1.xx&amp;oh=d49f23e8383b0992dcf01ebd8b6e7705&amp;oe=5DFFE2A1" alt="DB structure">
  Here fiddle with database: <a href="https://www.db-fiddle.com/f/sjhDVF9goxcvkpBVoxAE5M/1?fbclid=IwAR1C9NaWSDKw_qPp9JjUHw1gFiPI0aBqTQ49O-lT1cYeRrLFJA4iGt64zoU" rel="nofollow noreferrer">DB fiddle</a></p>
  
  <p>I have problem with first query:</p>
  
  <pre><code>**Query #1**
      =============================================
          List of all roles related to permission with id 3   */
      SELECT DISTINCT role_id, access, permission_id FROM role_permissions WHERE permission_id=3 ORDER BY role_id, access DESC;
  
  | role_id | access | permission_id |
  | ------- | ------ | ------------- |
  | 5       | 0      | 3             |
  | 8       | 1      | 3             |
  | 10      | 1      | 3             |
  | 10      | 0      | 3             |
  
  </code></pre>
  
  <p>As expected I should get </p>
  
  <pre><code>| role_id | access | permission_id |
  | ------- | ------ | ------------- |
  | 8       | 1      | 3             |
  </code></pre>
  
  <p>I cant add <code>WHERE permission_id=3 AND access=1</code>, because i getting result: role_id=8 and role_id=10, but role_id=10 doesn't really have access.</p>