Ask Your Question
3

How can we perform a SELECT query using a RIGHT JOIN in TypeORM?

asked 2023-05-22 18:04:29 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-22 18:24:02 +0000

pufferfish gravatar image

To perform a SELECT query using a RIGHT JOIN in TypeORM, we can use the following syntax:

const result = await getConnection()
    .createQueryBuilder()
    .select([
        'users.id',
        'users.name',
        'addresses.address'
    ])
    .from(User, 'users')
    .rightJoin(Address, 'addresses', 'addresses.userId = users.id')
    .where('users.id = :userId', { userId: 1 })
    .getRawMany();

In this example, we are selecting the id, name, and address fields from the users and addresses tables. We are also performing a right join between the two tables on the userId field. Finally, we are filtering the results to only return the row with id equal to 1. The getRawMany() method returns an array of raw database results.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-22 18:04:29 +0000

Seen: 7 times

Last updated: May 22 '23