假设我们有一个包含以下表格的数据库:
“艺术家”(列:名称,ID)和
“歌曲”(标题,composers_id,songwriters_id)。
我想用作曲家和作曲家的名字打印歌曲。我仅通过以下方式成功打印了作曲家的名字:
SELECT title, name AS Composers_Name
FROM artist, song
WHERE song.composers_id = artist.id;
Run Code Online (Sandbox Code Playgroud)
我无法获取歌曲作者的名字。
我尝试的是:
SELECT title, name AS Composers_Name, name AS Songwriters_name
FROM artist, song
WHERE song.composers_id = artist.id AND song.songwriters_id = artist.id;
Run Code Online (Sandbox Code Playgroud)
但这会返回作曲家和作曲家是同一个人的所有歌曲。我正在考虑使用JOIN,但不确定如何。
您必须从餐桌艺术家中选择2次。
select s.title, a1.name AS Composer, a2.name as songwriter
from song s, artist a1, artist a2
where s.composers_id = a1.id and s.songwriters_id = a2.id;
Run Code Online (Sandbox Code Playgroud)
假设作曲家和歌曲作者都存储在餐桌艺术家中。
| 归档时间: |
|
| 查看次数: |
8633 次 |
| 最近记录: |