Codeigniter选择连接给出重复的结果

时间:2017-11-15 08:30:40

标签: php postgresql codeigniter

大家好,所以我无法在codeigniter模型中创建一个查询,这会给我这个结果。到目前为止,我在我的数据库上有这些表。而且我正在尝试使用加入它并不适合我。它只会显示重复的歌曲和不同的发布者/作者。这是我模型中的当前查询。

public function get_songs_in_playlist($playlist_id)
{
    $this->db->select(
        "si.song_info_file_name, 
        si.song_info_revised_file_name, 
        a.artist_name, 
        si.song_info_id as songinfoid,
        array_to_string(array_agg(c.composer_name),'/'),
        array_to_string(array_agg(p.publisher_name),'/'), 
        array_to_string(array_agg(st.tag_name),',')"
    );
    $this->db->from('songs_in_playlist sp');
    $this->db->join('song_info si', 'sp.song_info_id = si.song_info_id', 'left');
    $this->db->join('original_artist a', 'si.artist_id = a.artist_id', 'left');
    $this->db->join('composer c', 'si.composer_id = c.composer_id', 'left');
    $this->db->join('publisher p', 'si.publisher_id = p.publisher_id', 'left');
    $this->db->join('song_info_tags sit', 'si.song_info_id = sit.song_info_id', 'left');
    $this->db->join('song_tags st', 'st.tag_id = sit.tag_id', 'left');
    $this->db->where('sp.playlist_id', $playlist_id);
    $this->db->group_by(array("si.song_info_id", "a.artist_name", "c.composer_name" , "p.publisher_name" ));

    $query = $this->db->get();


    return $query->result_array();

}

歌曲表

song_info_id   song_name
1              songA

发布商表

publisher_id   publisher_name
1              publisherA
2              publisherB

作家表

writer_id      writer_name     song_info_id    publisher_id
1              writerA         1               1
2              writerB         1               2

结果应该是:

Song Name      Writers                Publishers
SongA          writerA, writerB       publisherA, publisherB

加入的当前结果:

Song Name      Writers                Publishers
SongA          writerA                publisherA
SongA          writerB                publisherB

数组中的结果

Array ( 
    [0] => Array ( 
        [song_info_file_name] => Cartoon_-_On_On_(ft__Daniel_Levi).mp3 
        [song_info_revised_file_name] => 1510657326_Cartoon_-_On_On_(ft__Daniel_Levi).mp3 
        [status_artist_name] => Cartoon (feat. Daniel Levi) 
        [songinfoid] => 133 [title] => On & On (feat. Daniel Levi) 
        [array_to_string] => 
    ) 
    [1] => Array ( 
        [song_info_file_name] => Cartoon_-_On_On_(ft__Daniel_Levi).mp3 
        [song_info_revised_file_name] => 1510657326_Cartoon_-_On_On_(ft__Daniel_Levi).mp3 
        [status_artist_name] => American Authors 
        [songinfoid] => 133 
        [title] => On & On (feat. Daniel Levi) 
        [array_to_string] => 
    ) 
    [2] => Array ( 
        [song_info_file_name] => 01-Reverie_(feat__King_Deco).mp3 
        [song_info_revised_file_name] => 1510728747_01-Reverie_(feat__King_Deco).mp3 
        [status_artist_name] => Illenium 
        [songinfoid] => 136 
        [title] => Reverie (feat. King Deco) [array_to_string] => 
        ) 
    [3] => Array ( 
        [song_info_file_name] => 01-Reverie_(feat__King_Deco).mp3 
        [song_info_revised_file_name] => 1510728747_01-Reverie_(feat__King_Deco).mp3 
        [status_artist_name] => Cartoon (feat. Daniel Levi) 
        [songinfoid] => 136 
        [title] => Reverie (feat. King Deco) 
        [array_to_string] => 
    ) 
    [4] => Array ( 
        [song_info_file_name] => 01-Reverie_(feat__King_Deco).mp3 
        [song_info_revised_file_name] => 1510728747_01-Reverie_(feat__King_Deco).mp3 
        [status_artist_name] => American Authors 
        [songinfoid] => 136 
        [title] => Reverie (feat. King Deco) 
        [array_to_string] => 
    ) 
    [5] => Array ( 
        [song_info_file_name] => Cartoon_-_On_On_(ft__Daniel_Levi).mp3 
        [song_info_revised_file_name] => 1510657326_Cartoon_-_On_On_(ft__Daniel_Levi).mp3 
        [status_artist_name] => Illenium 
        [songinfoid] => 133 
        [title] => On & On (feat. Daniel Levi) 
        [array_to_string] => 
    ) 
    [6] => Array ( 
        [song_info_file_name] => American_Authors_-_Best_Day_Of_My_Life.mp3 
        [song_info_revised_file_name] => 1510540526_American_Authors_-_Best_Day_Of_My_Life.mp3 
        [status_artist_name] => Cartoon (feat. Daniel Levi) 
        [songinfoid] => 134 
        [title] => Best Day of My Life 
        [array_to_string] => Mix, Half, Alternative, Mid 
    ) 
    [7] => Array ( 
        [song_info_file_name] => American_Authors_-_Best_Day_Of_My_Life.mp3 
        [song_info_revised_file_name] => 1510540526_American_Authors_-_Best_Day_Of_My_Life.mp3 
        [status_artist_name] => American Authors 
        [songinfoid] => 134 
        [title] => Best Day of My Life [array_to_string] => Mix, Half, Alternative, Mid 
    ) 
    [8] => Array ( 
        [song_info_file_name] => American_Authors_-_Best_Day_Of_My_Life.mp3 
        [song_info_revised_file_name] => 1510540526_American_Authors_-_Best_Day_Of_My_Life.mp3 
        [status_artist_name] => Illenium 
        [songinfoid] => 134 
        [title] => Best Day of My Life 
        [array_to_string] => Mix, Mix, Half, Half, Alternative, Alternative, Mid, Mid 
    ) 
)

1 个答案:

答案 0 :(得分:0)

经过几分钟的脑力激荡,我找到了一种能够解决这个问题的方法。感谢那些评论的人。对不起标记mysql但我使用的是postgresql。这是我未来目的的解决方案。

 public function get_shareable_songs_playlist($playlist_id)
{
    $this->db->select("si.songinfoid,
    si.filename,
    si.revised_filename,
    si.title,

    array_to_string(
        ARRAY(
            select status_artist_name from status s where s.song_info_id = si.songinfoid  
        ),

    '/'
    ) AS artist,

    array_to_string(
        ARRAY(
            select c.composer_name from composer c, song_composer sc where c.composer_id = sc.composer_id AND sc.song_info_id = si.songinfoid  
        ),

    '/'
    ) AS composer,

    array_to_string(
        ARRAY(
            select p.publisher_name from publisher p, composer c, song_composer sc where p.publisher_id = c.publisher_id AND c.composer_id = sc.composer_id AND sc.song_info_id = si.songinfoid  
        ),

    '/'
    ) AS publisher,

    array_to_string(
        ARRAY(
            select s.tag_name from song_tags s, song_info_tags sit where s.tag_id = sit.tag_id AND sit.song_info_id = si.songinfoid AND sit.info_tag_delete_flag = 0
        ),

    '/'
    ) AS tags,
    sp.playlist_id");
    $this->db->from("(SELECT song_info_id AS songinfoid, song_info_file_name AS filename, song_info_revised_file_name AS revised_filename, song_info_original_title AS title FROM song_info si GROUP BY song_info_id)si");
    $this->db->join('songs_in_playlist sp', 'sp.song_info_id = si.songinfoid');
    $this->db->join('playlist play', 'sp.playlist_id = play.playlist_id');
$this->db->where('sp.playlist_id', $playlist_id);
$this->db->where('play.playlist_share', 1);
$query = $this->db->get();

return $query->result_array();
}