Saturday 27 May 2017

Node MySQL Left join the multiple rows into one

Currently, I'm using Node MySQL library to implement MySQL query in Javascript framework. I have an issue about the left join with multiple rows. When I left join that table, it returns more than one object, this is because the left join will produce duplicate rows but different values of that particular attribute. What I want to achieve right now is, returns one object and insert that multiple values to array of that particular attribute.

Table A

id | name | age
 1   abel   22
 2   john   22

Table B

id | user_id | equip
 1      1      armor
 2      2      sword
 3      1      knife

Query

SELECT * FROM Table_A LEFT JOIN TABLE_B ON TABLE_B.user_id = TABLE_A.id;

Current Situation

{
    id: 1
    name: abel
    age: 22
    user_id: 1
    equip: 'armor'
},
{
    id: 1
    name: abel
    age: 22
    user_id: 1
    equip: 'knife'
}

What I want to achieve

{
    id: 1
    name: abel
    age: 22
    user_id: 1
    equip: [
       'armor','knife'
    ]
}



via Abel Chun

No comments:

Post a Comment