1

I am new to query builder. I need to apply a query. I did it on mysql and it's working fine. But I couldn't make where condition in the query.

This is my SQL query:

select users_roles.user_id 
from users_roles 
inner join roles on roles.id = users_roles.role_id  
where roles.role = 'ADMIN_ROLE' 
   or roles.role = 'SUPER_ADMIN_ROLE'

I tried to do this using query builder and did this without using any where condition. How can I filter according to that mysql code? Here I am adding my query builder query:

const query = this.userRoleRepository.createQueryBuilder('usersRoles');

      const users = await query
        .innerJoinAndSelect('usersRoles.user', 'user')
        .getMany();

Actually it's returning whole user list. But I need only specified two type of user. That should be filtered with where condition. which is in mysql query.

1
  • Since you do have the actual query just use it. Commented Oct 25, 2021 at 6:51

1 Answer 1

1
const users = await query
    .select(userRoles.user_id)
    .innerJoin('userRoles.roles', 'roles', 'roles.id = users_roles.role_id')
    .where("roles.role = :adminRole OR roles.role =: superRole", { adminRole: 'ADMIN_ROLE', superRole: 'SUPER_ADMIN_ROLE' })
    .getMany();

I hope this works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.