I have three tables right now in a postgres DB:
Artists
ID Name
Shows
ID Date Venue Price Is_sold Ages Time Multi_day Pit
Artists_shows
ID Show_id Artist_id
A show can have one or many artists; eventually I'm going to produce JSON for all shows that looks like this:
[
{
"bands": [
"Domino And The Derelicts",
"Creeper",
"Lesbros"
],
"date": "2015-12-26T08:00:00.000Z",
"venue": "Stork Club, Oakland",
"time": null,
"soldOut": false,
"pit": false,
"multiDay": false,
"ages": "21+",
"price": null
}
...]
Right now I'm doing this pretty inefficiently.
First, I'm running a query: SELECT * FROM shows WHERE date >= ${date} ORDER BY date
, where date
is today's date.
Then, I'm taking the result and running a for loop through each individual show
that results and running this query: SELECT artist.* FROM artist INNER JOIN artist_shows ON artist.id = artist_shows.artist_id AND artist_shows.show_id=${id}
, where id
is each id
from the results of the first query on the Shows table.
I'm then stitching together the results of both queries to produce the resulting JSON.
Is there a way to perform a single JOIN
operation on my tables so that I can avoid the loop that I'm running? I assume that there might be a way to merge my existing join with my first query so that I get a table with all of the artists and all of the show details in one table. The thing that is tripping me up is the fact that a show can have many artists associated with it.