0

我正在尝试从 MySQL 中的两个表中获取列表。通过查看artist_albums_link -> artist_uid 并匹配到artist_albums。该列表应包含相同的专辑,但还应包含用户没有的艺术家的所有专辑。

user_uid = '5' 的所需结果

album_uid       artist_uid                      title       user_uid
album:another   artist:00N1TiCLsiP8IE24hw0rl9   yahoo       5
album:test      artist:00N1TiCLsiP8IE24hw0rl9   Test        5
album:notinlist artist:00N1TiCLsiP8IE24hw0rl9   add this    null

这是下面的数据表。注意用户 '5' 有两个专辑链接,但是因为艺术家还有一张专辑,所以它也被显示了。相册“未显示”不在用户相册链接中,因此不会出现在列表中。

表:艺术家_专辑

album_uid       artist_uid                      title
album:another   artist:00N1TiCLsiP8IE24hw0rl9   yahoo   
album:noshow    artist:00sazWvoTLOqg5MFwC68Um   not show    
album:notinlist artist:00N1TiCLsiP8IE24hw0rl9⇥  add this    
album:test      artist:00N1TiCLsiP8IE24hw0rl9   Test    

表:artist_albums_link

uid user_uid    artist_uid                      album_uid   
1   5           artist:00N1TiCLsiP8IE24hw0rl9   album:test  
2   6           artist:00N1TiCLsiP8IE24hw0rl9   album:test  
3   5           artist:00N1TiCLsiP8IE24hw0rl9   album:another   

尝试使用 join 获取空值但没有成功。该行的新值 a.updated (timestamp) 是正确的,但专辑“album:notinlist”的值 b.found (timestamp) 是错误的,此帖子在 Artist_albums_link 中不存在。

SELECT  a.album_uid, a.artist_uid, a.title, a.updated, b.user_uid,  b.found
FROM  artist_albums a LEFT OUTER JOIN artist_albums_link b ON a.artist_uid = b.artist_uid
WHERE b.user_uid = '5'

结果:

album_uid       artist_uid                      title       updated                 user_uid    found
album:another   artist:00N1TiCLsiP8IE24hw0rl9   yahoo       2013-03-20 11:29:08     5           2013-03-18 11:24:47
album:another   artist:00N1TiCLsiP8IE24hw0rl9   yahoo       2013-03-20 11:29:08     5           2013-03-19 11:29:59
album:notinlist artist:00N1TiCLsiP8IE24hw0rl9   add this    2013-03-15 11:41:20     5           2013-03-18 11:24:47
album:notinlist artist:00N1TiCLsiP8IE24hw0rl9   add this    2013-03-15 11:41:20     5           2013-03-19 11:29:59
album:test      artist:00N1TiCLsiP8IE24hw0rl9   Test        2013-03-20 11:23:41     5           2013-03-18 11:24:47
album:test      artist:00N1TiCLsiP8IE24hw0rl9   Test        2013-03-20 11:23:41     5           2013-03-19 11:29:59
4

2 回答 2

0

您可以尝试以下代码:

SELECT a.album_uid, a.artist_uid, a.title, b.user_uid
FROM  artists_albums a, artist_albums_link b
WHERE a.artist_uid = b.artist_uid  AND b.user_uid = 5;

希望能帮助到你!

于 2013-03-20T12:35:20.093 回答
0

终于找到了有用的东西。

SELECT a.album_uid, a.artist_uid, a.title, a.updated, b.user_uid, b.found
FROM artist_albums AS a
LEFT OUTER JOIN artist_albums_link AS b ON a.album_uid = b.album_uid
WHERE EXISTS (SELECT artist_uid FROM artist_albums_link WHERE artist_uid = a.artist_uid ) AND (b.user_uid = '5' OR b.user_uid is null)
于 2013-03-20T16:34:09.737 回答