艺术家桌:
Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL |歌曲表:
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(80) | YES | | NULL | |
| minutes | int(11) | YES | | NULL | |
| seconds | int(11) | YES | | NULL | |
| sales | decimal(5,2) | NO | | 0.00 | |
| genre_id | int(11) | NO | MUL | NULL |我的接线桌song_artist:
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| song_id | int(11) | NO | PRI | NULL | |
| artist_id | int(11) | NO | PRI | NULL所以我的资料里有没有歌曲的艺术家,数据里没有艺术家的歌曲。我可以让没有歌曲的艺术家出现在左边,然后右外连接,但我似乎无法以相反的方式获取数据。我一直在尝试使用这个代码来获得没有任何艺术家在我的桌子上的歌曲:
SELECT songs.title, artists.name
-> FROM songs
-> RIGHT JOIN song_artist
-> ON (songs.id = song_artist.song_id)
-> RIGHT JOIN artists
-> ON (artists.id = song_artist.artist_id);但它就是行不通。我需要所有歌曲和艺术家的名字的标题,以显示在我的结果。
就像这样我需要我的结果:
title | name |
+-----------------------+-------------------------------+
| Under Pressure | Queen |
| Let It Be | Beatles |
| Broadway | Goo Goo Dolls |
| I Hope You Dance | Lee Ann Womack |
| Turn Off The Light | Nelly Furtado |
| I'm Like a Bird | Nelly Furtado |
| Canon in D | Barrymoore Chamber Orchestra |
| Canon in D | London Philharmonic Orchestra |
| Star Wars | London Philharmonic Orchestra |
| Sea Drift | London Philharmonic Orchestra |
| NULL | Pop Evil |
| NULL | Volbeat |
| NULL | Godsmack |然而,我需要在我的名字类别,而不是在我的标题类别中的纽尔斯。
发布于 2019-11-17 16:09:13
要在没有任何艺术家的情况下获得歌曲,可以使用not exists
select s.*
from songs s
where not exists (
select 1
from song_artist sa
inner join artist a on a.id = sa.artist_id
where sa.song_id = s.id
)这包括以下情况:
对于给定的歌曲,artist在song_artist中没有条目,在song_artist中有一个(或多个)条目,但是在表中找不到引用的artist_id(s)。
https://stackoverflow.com/questions/58902425
复制相似问题