Mysql Laravel列出了受欢迎的子模型音乐类型
我有4张桌子:Mysql Laravel列出了受欢迎的子模型音乐类型,mysql,laravel,model,Mysql,Laravel,Model,我有4张桌子: MusicGenre Artist 歌曲 SongInfo weekly\u hit是SongInfo表中的一列 MusicGenre连接到Artist,如下所示: public function artists() { return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id'); } public function songs() { return $this->has
MusicGenre
Artist
歌曲
SongInfo
weekly\u hit
是SongInfo
表中的一列
MusicGenre
连接到Artist
,如下所示:
public function artists()
{
return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id');
}
public function songs()
{
return $this->hasMany('App\Models\Song\Songs','artist_id');
}
public function info()
{
return $this->hasOne('App\Models\Song\SongsInfo','song_id','id');
}
Artist
连接到Song
,如下所示:
public function artists()
{
return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id');
}
public function songs()
{
return $this->hasMany('App\Models\Song\Songs','artist_id');
}
public function info()
{
return $this->hasOne('App\Models\Song\SongsInfo','song_id','id');
}
而Song
连接到SongInfo
,如下所示:
public function artists()
{
return $this->hasMany('App\Models\Artist\ArtistInfo','genre_id','id');
}
public function songs()
{
return $this->hasMany('App\Models\Song\Songs','artist_id');
}
public function info()
{
return $this->hasOne('App\Models\Song\SongsInfo','song_id','id');
}
查询该表没有问题
我的问题是,我想使用SongInfo
表格中的weekly\u hit
获得最佳音乐类型
编辑:
我用原始代码解决了这个问题
"select music_genres.*,
sum(distinct song_info.weekly_hit) as song_popularity
from `music_genres`
left join `artist_info` on
`music_genres`.`id` = `artist_info`.`genre_id`
left join songs on
artist_info.artist_id = songs.artist_id
left join songs_info on
songs.id = songs_info.song_id
group by music_genres.name
order by song_popularity DESC
limit 5
但是,我找不到歌曲。我想从所有返回的音乐类型中获得5首歌曲,按歌曲信息表中的每周热门歌曲排序
伙计们,我还在寻找解决方案吗
有人能帮我吗?你需要使用Eloquent的hasManyThrough()关系来深入到关系链的深处。听起来你需要遍历
歌曲
,获得每首歌曲的歌曲id
,然后查询每个SongInfo
,在临时位置查找并记录weekly\u hit
,以及song\u id
。然后,您可以按每周点击次数对歌曲id进行排序。