I am new to SQL. I have a database with two tables: users and collections. The table 'users' stores the users of an app while 'collections' stores a collection of items. Each collection is owned by one user.
I need to get a JSON object with the collections and their owner.
For example:
[
{
"collection_id": 23,
"collection_name": "The Beatles Albums"
"owner": {
"user_id": 5,
"first_name": "John",
"last_name": "Doe"
}
}
]
This is what I tried:
router.get('/collections/', (req, res) => {
mysqlconnection.query('SELECT * FROM collections INNER JOIN users ON collections.OwnerID = users.id WHERE collections.OwnerID = users.id ', (err, rows, fields) => {
if(!err) {
res.json(rows);
} else {
console.log(err);
}
})
})
This is what I'm getting:
[
{
"collection_id": 23,
"collection_name": "The Beatles Albums",
"owner": 5,
"user_id": 5,
"first_name": "John",
"last_name": "Doe"
}
]
I'm new to SQL. Any help is greatly appreciated.