我有三个表:
歌曲表:
图片表:
我正在尝试显示一组歌曲及其标题和相应的图像。
如果歌曲没有图像(如果`image_id`列为`0`),那么我想只使用专辑图像。
目前我只有这段代码,我卡住了:
albums
、songs
和 images
。
这些表看起来像这样:
albums
表:id | title | image_id
5 | First Album | 1
6 | Another Album | 2
歌曲表:
songs
id | album_id | title | image_id
32 | 5 | My Song | 3
33 | 5 | Another One | 4
34 | 5 | First Song | 0
35 | 6 | My Song #2 | 0
36 | 6 | Fancy Title | 0
37 | 6 | My Love Song | 5
图片表:
images
id | path
1 | path/to/image1.jpg
2 | path/to/image2.jpg
3 | path/to/image3.jpg
4 | path/to/image4.jpg
5 | path/to/image5.jpg
我正在尝试显示一组歌曲及其标题和相应的图像。
如果歌曲没有图像(如果`image_id`列为`0`),那么我想只使用专辑图像。
目前我只有这段代码,我卡住了:
SELECT songs.title, images.path
FROM songs
LEFT JOIN images
ON images.id = songs.image_id
// ...if songs.image_id is 0, i want to just use the image_id of the corresponding album instead
如果它是一个能够处理大表格的高效查询,那就太好了。
COALESCE()
函数。 - PM 77-1