因此,我无法解决以下问题。给出了三个表:tables
我需要使用join编写代码,以便所有对朋友对均以其全名列出。我只知道可能需要两个联接,其中之一是自我联接。我试图将一个persons表与friends表连接起来,但是一栏中的结果是name,另一列是and id,我不知道如何进行两个连续的联接,以便每个名字都有一个对应的朋友的名字。这是表格信息:
CREATE TABLE persons (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fullname TEXT,
age INTEGER);
INSERT INTO persons (fullname, age) VALUES ("Bobby McBobbyFace", "12");
INSERT INTO persons (fullname, age) VALUES ("Lucy BoBucie", "25");
INSERT INTO persons (fullname, age) VALUES ("Banana FoFanna", "14");
INSERT INTO persons (fullname, age) VALUES ("Shish Kabob", "20");
CREATE table hobbies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
person_id INTEGER,
name TEXT);
INSERT INTO hobbies (person_id, name) VALUES (1, "drawing");
INSERT INTO hobbies (person_id, name) VALUES (2, "dancing");
INSERT INTO hobbies (person_id, name) VALUES (3, "skating");
INSERT INTO hobbies (person_id, name) VALUES (4, "coding");
CREATE table friends (
id INTEGER PRIMARY KEY AUTOINCREMENT,
person1_id INTEGER,
person2_id INTEGER);
INSERT INTO friends (person1_id, person2_id)
VALUES (1, 4);
INSERT INTO friends (person1_id, person2_id)
VALUES (2, 3);
答案 0 :(得分:0)
您是否正在寻找两个联接?
select p1.fullname, p2.fullname
from friends f join
persons p1
on f.person1_id = p1.id join
persons p2
on f.person2_id = p2.id;