Question : cfml SQL MSSQL question

     

     
            SELECT rtrim(first_name) + ' ' + rtrim(last_name) as musician, user_id from users
            WHERE church_id = #church_id# and authority_id = 4 order by first_name


the above is the old sql query, which populated a list based on the authority level of a user, which was a column in the table "users". (in the old way, column authority_id = 4 meant that the user was a musician, and their name was pulled from the database using this query.)

I have updated the application, so that users now have multiple roles, and their roles are no longer based on the authority_id column in the users, but from the table 'roles_user.'

Roles come from the table roles_user, which looks like  this:

site_id    user_id    role_id
10          244            9
10          244            4
10          244            1
10         245            1
10         246           1
10        247            1

(site_id = church_id, and is a part of all queries, because the app drives multiple "instances" of sites, each of which have unique users id's


the table "roles"

role_id      site_id     role_desc

1              10            musician
4              10            pastor
9              10            board member

I don't want to set the defining variable as role_id = 9, because this will force this variable to always be 9 in any hardcoded code, rather, I want it set to role_desc = musician, so that the role_id can be flexible when it is setup in the database. This way, for different site_id/church_id's the "musician" role can have any  role_id, depending on how it was setup, but the ap will always work. Role_id's will probably always be incremental in terms of their addition, so a site_id = 23 could have a corresponding musician role as 70, or however many roles for however many total sites have been added up to that point.

for reference

table "users" looks like this:


User_id    Church_id    Authority_ID     First_Name     Last_Name

244      10                    1                          elvis              presley
245         10                      2                           james         dean
246      10                   3                        santa            claus
247      10                  4                       ronald           reagan



this might be overkill, but with the old way, only ronald reagan (with authority_id = 4) would be defined as a musican, but with the new way,  they would all be musicans, because, 244, 245, 246, and 247 are in table roles_user with column role_id = 9, which resolves 1 in table "roles"

The as defined above will still have the same output, but I'm not good at the sql joins that will need to be implemented to change the query from  the old to the new way of keeping track of who musicians are

Answer : cfml SQL MSSQL question

If the values are sequential for user_id and role_id then you will probably not need the additional joins on site_id/church_id, but shouldn't hurt either way; therefore, I included those in both join clauses.  
1:
2:
3:
4:
5:
6:
7:
8:
9:
      
   SELECT rtrim(u.first_name) + ' ' + rtrim(u.last_name) AS musician, u.user_id 
   FROM users u
   INNER JOIN roles_user ru ON ru.user_id = u.user_id AND ru.site_id = u.church_id
   INNER JOIN roles r ON r.role_id = ru.role_id AND r.site_id = ru.site_id
   WHERE u.church_id = #church_id# 
   	AND r.role_desc = 'musician'
   ORDER BY u.first_name

Open in New Window Select All
Random Solutions  
 
programming4us programming4us