我想從多個具有多個外鍵的表中選擇sqlite中的結果,我試過JOIN
,但它不能很好地工作;例如:如何從多個具有多個外鍵的表中選擇sqlite結果
爲STUDENT
表:
CREATE TABLE STUDENT (
STUDENT_NAME TEXT NOT NULL,
STUDENT_NUMBER INTEGER PRIMARY KEY NOT NULL,
STUDENT_ADDRESS TEXT NOT NULL
);
和EXAMS
表:
CREATE TABLE EXAMS(
EXAM_CODE INTEGER PRIMARY KEY NOT NULL,
EXAM_SUBJECT TEXT NOT NULL,
EXAM_LOCATION TEXT NOT NULL
);
和一個叫WROTE_EXAM
讓學生是誰寫的具體考試信息表
CREATE TABLE WROTE_EXAM (
STUDENT_NUMBER INTEGER NOT NULL,
EXAM_CODE INTEGER NOT NULL,
DATE DATE NOT NULL,
FOREIGN KEY(STUDENT_NUMBER) REFERENCES STUDENT(STUDENT_NUMBER),
FOREIGN KEY(EXAM_CODE) REFERENCES EXAMS(EXAM_CODE));
這是一個樣本數據i nserted到表:
STUDENT_NAME : John
STUDENT_NUMBER: 123456789
STUDENT_ADDRESS : 10th street
EXAM_CODE: 123
EXAM_SUBJECT: One Subject
EXAM_LOCATION: Class
現在,我想:
一)輸出學生姓名,考試代碼和學生的位置,誰寫的考試
B)輸出考試代碼,考試科目和考試地點爲學生與ID:123456789
感謝
你嘗試過什麼JOIN? –
@ muistooshort我做了'SELECT DISTINCT STUDENT_NAME,EXAM_CODE,STUDENT_LOCATION FROM WROTE_EXAM,STUDENT JOIN EXAMS,STUDENTS'不知道如何將3張桌子連接在一起並獲取此信息 –