-2

song_id と uid が与えられた場合、同じ曲が好きなすべての友達 (直接の友達と友達の友達) を知りたいとします。

これが私たちのやり方です:

public function get_song_mates($uid, $song_id, $current_only = true, $limit = PHP_INT_MAX)
    {

        $sql    = "SELECT * 
                    FROM (
                    (
                        SELECT users . * ,  '1st' AS  `level`, 1 AS `level_i` 
                        FROM songs
                        JOIN users ON users.id = songs.user_id
                        JOIN friends AS my_friend ON my_friend.fid = users.id 
                        AND my_friend.uid = '".$uid."'                  
                        WHERE song_id =  '".$song_id."')
                    UNION (

                    SELECT second_friend . * ,  '2nd' AS  `level`, 2 AS `level_i`
                    FROM songs
                    JOIN users AS second_friend ON second_friend.id = songs.user_id
                    JOIN friends ON friends.uid = second_friend.id
                    JOIN users AS first_friend ON first_friend.id = friends.fid 
                    JOIN friends AS my_friend ON my_friend.fid = first_friend.id 
                    AND my_friend.uid = '".$uid."'                  
                    WHERE song_id =  '".$song_id."'


                    AND second_friend.id NOT IN
                        (
                            SELECT users.id 
                              FROM songs
                              JOIN users ON users.id = songs.user_id
                              JOIN friends AS my_friend ON my_friend.fid = users.id 
                               AND my_friend.uid = '".$uid."'
                             WHERE song_id = '".$song_id."'
                        )
                    ) ORDER BY `level_i` ASC, firstname, lastname
                    ) AS friend                 
                    GROUP BY id ORDER BY `level_i`";

        $users  = $this->getFromSql($sql);  
        return $users;
    }

ただし、以下を追加する必要があることに注意してください。

AND second_friend.id NOT IN...

同じユーザーを直接の友人および友人の友人として表示しないようにするには.. GROUP BYを使用する必要があると確信していますが、正しい構文を取得できません。ここに手がかりはありますか?

-編集-

friends(uid, fid)
songs(id, user_id, song_id)
users(id, frist_name, last_name)
4

1 に答える 1

0

次のように修正しました。

public function get_song_mates($uid, $song_id, $current_only = true, $limit = PHP_INT_MAX) {

    $sql    = "SELECT DISTINCT * 
                FROM (
                (
                    SELECT users . * ,  '1st' AS  `level`, 1 AS `level_i` 
                    FROM songs
                    JOIN users ON users.id = songs.user_id
                    JOIN friends AS my_friend ON my_friend.fid = users.id 
                    AND my_friend.uid = '".$uid."'                  
                    WHERE song_id =  '".$song_id."')
                UNION (

                SELECT DISTINCT second_friend . * ,  '2nd' AS  `level`, 2 AS `level_i`
                FROM songs
                JOIN users AS second_friend ON second_friend.id = songs.user_id
                JOIN friends ON friends.uid = second_friend.id
                JOIN users AS first_friend ON first_friend.id = friends.fid 
                JOIN friends AS my_friend ON my_friend.fid = first_friend.id 
                AND my_friend.uid = '".$uid."'                  
                WHERE song_id =  '".$song_id."'";

    $users  = $this->getFromSql($sql);  
    return $users;
}
于 2012-05-22T21:37:00.583 に答える