1、准备工作并插入数据:
创建两张表:student和class
CREATE TABLE CLASS
(
ID INT PRIMARY KEY,
NAME VARCHAR2(50)
);
CREATE TABLE STUDENT
(
STUNO INT PRIMARY KEY ,
STUNAME VARCHAR2(50),
CLASS_ID INT,
CONSTRAINT FK_STU_CLASS FOREIGN KEY (CLASS_ID)
REFERENCES CLASS(ID)
);
查询表情况如下:
使用左连接的的SQL语句:
SELECT C.ID CLASS_ID,C.NAME CLASS_NAME,S.STUNAME STUDENT_NAME FROM CLASS C LEFT JOIN STUDENT S ON C.ID=S.CLASS_ID;
使用内连接的SQL语句:
SELECT C.ID CLASS_ID,C.NAME CLASS_NAME,S.STUNAME STUDENT_NAME FROM CLASS C JOIN STUDENT S ON C.ID=S.CLASS_ID;
执行后的结果如下图: