我有两张桌子!
歌曲:
|---------------------|------------------|------------------|
| Title | ID | Version |
|---------------------|------------------|------------------|
| songnumber1 | 34 | original |
|---------------------|------------------|------------------|
| songnumber2 | 35 | remix |
|---------------------|------------------|------------------|
| songnumber3 | 36 | remix |
|---------------------|------------------|------------------|
我有其他表格可以在歌曲之间进行参考 参考:
|---------------------|------------------|------------------|
| ID | remix | original |
|---------------------|------------------|------------------|
| 1 | 35 | 34 |
|---------------------|------------------|------------------|
| 2 | 36 | 34 |
|---------------------|------------------|------------------|
我想要的是显示当前歌曲下面的相关歌曲 让我们说$ id是当前歌曲的ID,即35 我想显示所有其他歌曲混音和原版 我尝试了这个,但如果$ id是remix:
,它只会得到原始版本SELECT * FROM reference
INNER JOIN songs ON reference.remix = songs.id OR
reference.original = songs.id
WHERE reference.original = '$id' OR reference.remix = '$id'
答案 0 :(得分:0)
我认为这就是你想要的:
SELECT * FROM reference r
INNER JOIN songs a ON r.remix = a.ID
INNER JOIN songs b ON r.original = b.ID
WHERE r.remix = 35
如果您发布预期结果会有所帮助。也许这就是你想要的:
SELECT * FROM reference r
INNER JOIN Songs a ON r.remix = a.ID
WHERE r.remix = 35
UNION
SELECT * FROM reference r
INNER JOIN Songs b ON r.original = b.ID
WHERE r.remix = 35
答案 1 :(得分:0)
试试这个:
SELECT
C.currently_playing,
GROUP_CONCAT(DISTINCT IF(C.currently_playing=C.id,C.title,null)) current_song_title,
GROUP_CONCAT(DISTINCT IF(C.version='original',C.id,null)) orignial_song_id,
GROUP_CONCAT(DISTINCT IF(C.version='original',C.title,null)) orignial_song_title,
GROUP_CONCAT(DISTINCT IF(C.version='remix',C.id,null)) related_remix_ids,
GROUP_CONCAT(DISTINCT IF(C.version='remix',C.title,null)) related_remix_titles
FROM
(SELECT 35 AS currently_playing, A.*
FROM Songs A
RIGHT JOIN (SELECT * FROM `references` WHERE remix=35 or original=35) B
ON A.id IN (B.original,B.remix)) C
GROUP BY C.currently_playing;
查看有效的Demo on SQL Fiddle。