Home > Back-end >  how to write the right query to get only needed fields (Nest, MySQL)
how to write the right query to get only needed fields (Nest, MySQL)

Time:08-31

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

CodePudding user response:

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

  • Related