mysql连接别名为同一个表

mysql连接别名为同一个表,mysql,sql,join,Mysql,Sql,Join,我有一本带唱片的音乐目录。每个录音都有一个所有者和一个作者。各表如下: recordings recordings_authors(Fk rec_id, Fk contact_id) recordings_owners(Fk rec_id, Fk contact_id) contacts (name of people) 最终回波将显示: Title: (name of title) Author: (name from contacts) Owner: (name from contacts

我有一本带唱片的音乐目录。每个录音都有一个所有者和一个作者。各表如下:

recordings
recordings_authors(Fk rec_id, Fk contact_id)
recordings_owners(Fk rec_id, Fk contact_id)
contacts (name of people)
最终回波将显示:

Title: (name of title)
Author: (name from contacts)
Owner: (name from contacts)
以下作品很好,但所有者显示为与作者相同的联系人。完全删除JOIN author仍然会导致错误

SELECT recordings.title,contactsauthors.name AS authorname,contactsowners.name AS ownername
FROM recordings

JOIN recordings_authors AS authors ON recordings.id=authors.rec_id
JOIN contacts AS contactsauthors ON authors.rec_id=contactsauthors.id

JOIN recordings_owners AS owners ON recordings.id=owners.rec_id 
JOIN contacts AS contactsowners ON owners.rec_id=contactsowners.id

WHERE recordings.id=1


$row=mysqli_fetch_assoc($results);
echo $row['title'].'<h2>Credits</h2>Author: '.$row['authorname'].'<br>Owner: '.$row['ownername'];
选择录制。标题,ContactsAuthorName为ContactsAuthorName,contactsowners.name为ownername
从录音中
将录制作者作为录制作者加入。id=authors.rec\u id
将联系人作为联系人加入作者。rec_id=contactsauthors.id
将录制所有者作为录制的所有者加入。id=owners.rec\u id
将联系人作为联系人加入所有者。rec_id=contactsowners.id
其中,recordings.id=1
$row=mysqli\u fetch\u assoc($results);
echo$row['title']。'CreditsAuthor:'。$row['authorname']。
所有者:'.$row['ownername'];
我在这里能找到的最好答案就是这个
您在联系人连接中使用了错误的键。您应该使用
contact\u id
而不是
rec\u id

SELECT recordings.title,contactsauthors.name AS authorname,contactsowners.name AS ownername

FROM recordings

JOIN recordings_authors AS authors ON recordings.id=authors.rec_id 
JOIN contacts AS contactsauthors ON authors.contact_id=contactsauthors.id
-- here ----------------------------------> ^^^^^^^^^^

JOIN recordings_owners AS owners ON recordings.id=owners.rec_id 
JOIN contacts AS contactsowners ON owners.contact_id=contactsowners.id
-- here --------------------------------> ^^^^^^^^^^

WHERE recordings.id=1

您在联系人连接中使用了错误的密钥。您应该使用
contact\u id
而不是
rec\u id

SELECT recordings.title,contactsauthors.name AS authorname,contactsowners.name AS ownername

FROM recordings

JOIN recordings_authors AS authors ON recordings.id=authors.rec_id 
JOIN contacts AS contactsauthors ON authors.contact_id=contactsauthors.id
-- here ----------------------------------> ^^^^^^^^^^

JOIN recordings_owners AS owners ON recordings.id=owners.rec_id 
JOIN contacts AS contactsowners ON owners.contact_id=contactsowners.id
-- here --------------------------------> ^^^^^^^^^^

WHERE recordings.id=1

非常感谢chue x,这非常有效。事实上,只有联系人id不正确。非常感谢chue x,这非常有效。事实上,只有联系人id不正确。