how to write the right query to get only needed fields (Nest, MySQL)

Issue

i have two tables: users and freelancer, which connected one to one. I’m trying to query data from freelancer table, and I also need one field from users table. But with code like this:

.createQueryBuilder('freelancer')
    .leftJoinAndSelect('freelancer.user', 'users')
    .where(`freelancer.userId = ${id}`)
    .getOne()

I recive all data from users table. I tryed make it in diffirent ways with leftJoinAndMapOne but I have errors everytime. I can’t figure out how to write correctly. this is query what i’ve got, but I need something like this

Solution

Have you tried inner join if not then try it, and also you are using some dots instead of commas try them

for example you are doing this in your 2nd image that you have shared

SELECT freelancer.*,users.email from freelancer left join users on freelancer.userId=users.id;

instead you can try it like

SELECT f.id,*,u.id,u.email from freelancer f inner join users u on f.userId=u.id;

try this and let me know i will update it accordingly

Answered By – ammad youasf

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published