0

我想在 iOS/android 应用程序中集成类似的功能。我正在为此开发服务。我的问题是:我有一张名为歌曲的表格,其中有歌曲的详细信息

**like**

song
song_id
station_id
song_file

很快。我有第二个名为 userlike 的表,其中存储了用户喜欢的信息。

**userlike**
user_id
song_id
like

我的主要问题是我想返回所有歌曲详细信息以及特定用户的类似值信息。如果你不喜欢它,那么我可以在我的 JSON 中发送“零”或 null。如何构建此查询?如果用户喜欢它,我需要歌曲数据和喜欢的价值。

4

4 回答 4

0
select songs.song_id as song_id , songs.station_id as station_id , songs.song_file as song_file , userlike.user_id as user_id ,  COALESCE(userlike.like, 0) as like 
            FROM songs LEFT JOIN userlike
            on songs.song_id = userlike.song_id
            and userlike.user_id = 'your particular user'

The above sql should work. Please look for table name and column names for correction.

Thanks

于 2013-04-01T12:24:00.083 回答
0

尝试这个

select s.song, s.song_id from song s , userlike u where u.user_id = your userId;
于 2013-04-01T12:16:07.367 回答
0

假设userlike.likeboolean价值

SELECT like.song, like.song_id,
 like.station_id,
 like.song_file,
IFNULL(userlike.like,0)
from like left join userlike on like.song_id=userlike.song_id
and userlike.user_id=/*your userid*/
于 2013-04-01T12:20:20.727 回答
0

听起来你需要使用OUTER JOIN

SELECT s.song_id, s.song, u.like
FROM song s 
    LEFT JOIN userlike u 
        ON s.song_id = u.song_id 
            AND u.user_id = @userId

将 userId 放入 Join 将确保返回歌曲,即使用户不喜欢它。

于 2013-04-01T12:20:40.863 回答