title: MySQL Joining Table with Filter
body: <p>I'm writing a user-group-system. Here is my database structure:</p>
  
  <pre><code>users(id username group_id)
  -------- -------- --------
  1        gesting  1
  
  groups(id title)
  --------- -----
  1         Admin
  
  permissions(id value)
  -------------- --------------
  1              testpermission
  
  user_group_properties
  group_id user_id permission_id data
  -------- ------- ------------- -------------------------
  1        NULL    1             some further information
  NULL     1       1             some further information
  </code></pre>
  
  <p>Every user is in a group and every group has one entry in the user_group_properties-table.
  Now I want to start at the permissions-table and want to join in the user_group_properties-table. The user-id and the group-id is given.
  If the user has an own row in the user_group_properties-table (at least one entry "where user_id = {user_id}", then I want to get this row, otherwise I want to get the row of the group.</p>
  
  <p>In the sample data you can see a permission with id 1. In the user_group_properties-table are two entries with permission_id 1. In the first entry the user_id is null and the group_id not, in the second entry the group_id is null and the user_id not.
  If I'm doing a Join-Query now the MySQL-Server will give me the first row, the row where the user-id is null, but I need the row where the user-id is not null if such a row exists. If now row exists where the user-id is null I need the row of the group.</p>
  
  <p>I hope you can help me.</p>
  
  <p>Thanks a lot!</p>