left join when counting parent's children

This commit is contained in:
sentriz
2020-03-20 14:15:21 +00:00
parent 9ad0c2a3ac
commit ed4f55a21f
6 changed files with 96 additions and 96 deletions

View File

@@ -23,7 +23,7 @@ func (c *Controller) ServeGetIndexes(r *http.Request) *spec.Response {
var folders []*db.Album
c.DB.
Select("*, count(sub.id) child_count").
Joins("JOIN albums sub ON albums.id=sub.parent_id").
Joins("LEFT JOIN albums sub ON albums.id=sub.parent_id").
Where("albums.parent_id=1").
Group("albums.id").
Find(&folders)
@@ -137,7 +137,7 @@ func (c *Controller) ServeGetAlbumList(r *http.Request) *spec.Response {
// of children. it might make sense to store that in the db
q.
Select("albums.*, count(tracks.id) child_count").
Joins("JOIN tracks ON tracks.album_id=albums.id").
Joins("LEFT JOIN tracks ON tracks.album_id=albums.id").
Group("albums.id").
Where("albums.tag_artist_id IS NOT NULL").
Offset(params.GetIntOr("offset", 0)).

View File

@@ -18,7 +18,7 @@ func (c *Controller) ServeGetArtists(r *http.Request) *spec.Response {
var artists []*db.Artist
c.DB.
Select("*, count(sub.id) album_count").
Joins("JOIN albums sub ON artists.id=sub.tag_artist_id").
Joins("LEFT JOIN albums sub ON artists.id=sub.tag_artist_id").
Group("artists.id").
Find(&artists)
// [a-z#] -> 27
@@ -140,7 +140,7 @@ func (c *Controller) ServeGetAlbumListTwo(r *http.Request) *spec.Response {
// of children. it might make sense to store that in the db
q.
Select("albums.*, count(tracks.id) child_count").
Joins("JOIN tracks ON tracks.album_id=albums.id").
Joins("LEFT JOIN tracks ON tracks.album_id=albums.id").
Group("albums.id").
Where("albums.tag_artist_id IS NOT NULL").
Offset(params.GetIntOr("offset", 0)).
@@ -257,7 +257,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
err = c.DB.
Select("artists.*, count(albums.id) album_count").
Where("name=?", similarInfo.Name).
Joins("JOIN albums ON artists.id=albums.tag_artist_id").
Joins("LEFT JOIN albums ON artists.id=albums.tag_artist_id").
Group("artists.id").
Find(artist).
Error

Binary file not shown.

View File

@@ -17,6 +17,17 @@
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "19",
"coverArt": "19",
"artist": "Ten Years After",
"title": "(1967) Ten Years After",
"parent": "18",
"isDir": true,
"songCount": 15,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "8",
"coverArt": "8",
@@ -28,6 +39,17 @@
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "5",
"coverArt": "5",
"artist": "A Certain Ratio",
"title": "(1994) The Graveyard and the Ballroom",
"parent": "4",
"isDir": true,
"songCount": 14,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "16",
"coverArt": "16",
@@ -51,13 +73,13 @@
"created": "0001-01-01T00:00:00Z"
},
{
"id": "9",
"coverArt": "9",
"artist": "13th Floor Lowervators",
"title": "(1966) The Psychedelic Sounds of the 13th Floor Elevators",
"parent": "7",
"id": "21",
"coverArt": "21",
"artist": "Captain Beefheart",
"title": "(1970) Lick My Decals Off, Bitch",
"parent": "20",
"isDir": true,
"songCount": 21,
"songCount": 15,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
@@ -72,39 +94,6 @@
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "5",
"coverArt": "5",
"artist": "A Certain Ratio",
"title": "(1994) The Graveyard and the Ballroom",
"parent": "4",
"isDir": true,
"songCount": 14,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "19",
"coverArt": "19",
"artist": "Ten Years After",
"title": "(1967) Ten Years After",
"parent": "18",
"isDir": true,
"songCount": 15,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "21",
"coverArt": "21",
"artist": "Captain Beefheart",
"title": "(1970) Lick My Decals Off, Bitch",
"parent": "20",
"isDir": true,
"songCount": 15,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "6",
"artist": "A Certain Ratio",
@@ -114,6 +103,17 @@
"songCount": 9,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
},
{
"id": "9",
"coverArt": "9",
"artist": "13th Floor Lowervators",
"title": "(1966) The Psychedelic Sounds of the 13th Floor Elevators",
"parent": "7",
"isDir": true,
"songCount": 21,
"duration": 0,
"created": "0001-01-01T00:00:00Z"
}
]
}

View File

@@ -7,20 +7,11 @@
"albumList2": {
"album": [
{
"id": "21",
"coverArt": "21",
"artistId": "7",
"artist": "Captain Beefheart & His Magic Band",
"name": "Lick My Decals Off, Baby",
"songCount": 15,
"duration": 0,
"created": "2019-06-10T19:26:30.944742894+01:00"
},
{
"id": "6",
"artistId": "2",
"artist": "A Certain Ratio",
"name": "To Each...",
"id": "13",
"coverArt": "13",
"artistId": "4",
"artist": "Anikas",
"name": "Anika",
"songCount": 9,
"duration": 0,
"created": "2019-05-23T15:12:02.921473302+01:00"
@@ -35,26 +26,6 @@
"duration": 0,
"created": "2019-04-30T16:48:48+01:00"
},
{
"id": "3",
"coverArt": "3",
"artistId": "1",
"artist": "Jah Wobble, The Edge & Holger Czukay",
"name": "Snake Charmer",
"songCount": 5,
"duration": 0,
"created": "2019-05-16T22:10:52+01:00"
},
{
"id": "13",
"coverArt": "13",
"artistId": "4",
"artist": "Anikas",
"name": "Anika",
"songCount": 9,
"duration": 0,
"created": "2019-05-23T15:12:02.921473302+01:00"
},
{
"id": "8",
"coverArt": "8",
@@ -75,6 +46,25 @@
"duration": 0,
"created": "2019-06-13T12:57:24.306717554+01:00"
},
{
"id": "6",
"artistId": "2",
"artist": "A Certain Ratio",
"name": "To Each...",
"songCount": 9,
"duration": 0,
"created": "2019-05-23T15:12:02.921473302+01:00"
},
{
"id": "3",
"coverArt": "3",
"artistId": "1",
"artist": "Jah Wobble, The Edge & Holger Czukay",
"name": "Snake Charmer",
"songCount": 5,
"duration": 0,
"created": "2019-05-16T22:10:52+01:00"
},
{
"id": "5",
"coverArt": "5",
@@ -85,16 +75,6 @@
"duration": 0,
"created": "2019-06-05T17:46:37.675917974+01:00"
},
{
"id": "16",
"coverArt": "16",
"artistId": "5",
"artist": "Swell Maps",
"name": "Jane From Occupied Europe",
"songCount": 16,
"duration": 0,
"created": "2019-04-30T16:48:48+01:00"
},
{
"id": "19",
"coverArt": "19",
@@ -104,6 +84,26 @@
"songCount": 15,
"duration": 0,
"created": "2019-04-30T16:48:30+01:00"
},
{
"id": "21",
"coverArt": "21",
"artistId": "7",
"artist": "Captain Beefheart & His Magic Band",
"name": "Lick My Decals Off, Baby",
"songCount": 15,
"duration": 0,
"created": "2019-06-10T19:26:30.944742894+01:00"
},
{
"id": "16",
"coverArt": "16",
"artistId": "5",
"artist": "Swell Maps",
"name": "Jane From Occupied Europe",
"songCount": 16,
"duration": 0,
"created": "2019-04-30T16:48:48+01:00"
}
]
}