假设我们有一个包含表的数据库:
“艺术家”(列:姓名、ID)和
“歌曲”(标题、composers_id、songwriters_id)。
我想打印带有 Composer 姓名和词曲作者姓名的歌曲。 我已经成功地打印了 Composer 的名字:
SELECT title, name AS Composers_Name
FROM artist, song
WHERE song.composers_id = artist.id;
我无法获取词曲作者的名字..
我试过的是这样的:
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;
但是这会返回作曲者和词曲作者是同一个人的所有歌曲。 我正在考虑使用 JOIN,但我不确定如何......
最佳答案
您必须从 table artist 中选择 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;
假设 Composers 和 Songwriters 都存储在表 artist 中。
关于MySQL,有没有办法使用多个别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13537170/