lundi 17 octobre 2016

SQL insert statement which copies from another row

Im wondering if someone could post some mock sql so i can see how to layout a query to solve the below situation.

I have a table and in this table are permissions for users to be able to access a resource from within a Room. Each room will have an individual row for each UserName - if UserName doesnt exists in this table for a room then they have no permissions in that room. Also each room should have a row which lists the UserName 'frank' which is the default room permissions given to everyone. The permissions are Boolean values (Perm1, Perm2....).

Now imagining that the table looks like this -

|UserName|Room|Perm1|Perm2|
---------------------------
|FRANK   | 1  | 0   |  1  | 
---------------------------
|ANNE    | 1  | 0   |  1  | 
---------------------------
|PETE    | 1  | 1   |  1  | 
---------------------------
|FRANK   | 2  | 1   |  1  | 
---------------------------
|FRANK   | 3  | 1   | 0   | 
---------------------------
|JOHN    | 4  | 0   |  0  | 
---------------------------
|FRANK   | 4  | 0   |  0  | 
---------------------------

What i would like to know how to do via SQL is

  1. Check if the default user Frank exists for each room
  2. If above is True then check if a user ANNE also exists
  3. if above IS true then update the default permissions of Frank to ANNE
  4. If ANNE doesn't exist then insert the user ANNE and copy the default permissions from Frank

Im only looking for the logic and structure of how i would complete this, not the actual answer.

atm i just cant think of how to go about this via SQL

Thanks for your time and i hope this makes sense.

Aucun commentaire:

Enregistrer un commentaire