需要在mysql中基于条件连接表

时间:2022-09-05 15:42:18

I want to integrate like functionality in iOS/android app. I am developing service for that. My question is : I have table called songs in which I have song details

我想在iOS / Android应用程序中集成类似的功能。我正在为此开发服务。我的问题是:我有一张名为歌曲的桌子,其中有歌曲细节

**like**

song
song_id
station_id
song_file

so on. I have second table called userlike where user like inforamation are stored.

等等。我有第二个名为userlike的表,其中存储了像inforamation这样的用户。

**userlike**
user_id
song_id
like

My main problem is that I want to return all song details with like value information for particular user.If uer has not like it then I may send 'zero' or null in my JSON. How to build this query? I need song data and like value if user has liked it.

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

4 个解决方案

#1


0  

try this

select s.song, s.song_id from song s , userlike u where u.user_id = your userId;

#2


0  

assuming userlike.like is boolean value

假设userlike.like是布尔值

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*/

#3


0  

Sounds like you need to use an OUTER JOIN:

听起来你需要使用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

Putting the userId in the Join will ensure the song is returned even if the user hasn't liked it.

将userId置于Join中将确保即使用户不喜欢它也会返回歌曲。

#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.

上面的sql应该可行。请查找表名和列名进行更正。

Thanks

#1


0  

try this

select s.song, s.song_id from song s , userlike u where u.user_id = your userId;

#2


0  

assuming userlike.like is boolean value

假设userlike.like是布尔值

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*/

#3


0  

Sounds like you need to use an OUTER JOIN:

听起来你需要使用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

Putting the userId in the Join will ensure the song is returned even if the user hasn't liked it.

将userId置于Join中将确保即使用户不喜欢它也会返回歌曲。

#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.

上面的sql应该可行。请查找表名和列名进行更正。

Thanks