title: Insert record for each pair in two selects
body: <p>Currently I have 3 tables. Let's say users, permissions and table linking them both (because of <code>n</code> to <code>n</code> relationship) which can be called usr_prm. Now I have a group of users which should get certain set of permissions. I can define both groups in terms of sql selects. But now I'm wondering is there a way to do this operation in a single insert query? So far I have come up with the following solution, but this means running <code>x</code> queries, where <code>x</code> is the number of permissions I need to add. </p>
  
  <p>To add to the complexity I can say that set of users is chosen based on their already assigned permissions. So we can express it simple as: Add set of permissions (i.e. PERM_1_1, PERM_1_2) to the users who already have a permission PERM_1.</p>
  
  <p>I'm using PostgreSQL DB now but I hope there is universal solution because our application also supports SQL Server and Oracle DBs for some clients and it may come handy to use this script on their DBs too.</p>
  
  <p>What am I currently using:</p>
  
  <pre class="lang-sql prettyprint-override"><code>insert into usr_prm (user_id, permission_id)
  select users.id, (select id from permissions where name="PERM_1_1")
  from users
      inner join usr_prm on usr_prm.user_id = users.id
      inner join permissions on permissions.id = usr_prm.permission_id
  where permissions.name = "PERM_1"
    and ...
  
  insert into usr_prm (user_id, permission_id)
  select users.id, (select id from permissions where name="PERM_1_2")
  from users
      inner join usr_prm on usr_prm.user_id = users.id
      inner join permissions on permissions.id = usr_prm.permission_id
  where permissions.name = "PERM_1"
    and ...
  </code></pre>