SQL的子查询与JOIN的小试牛刀
//学生表
CREATE TABLE student( ID INT PRIMARY KEY, s_name VARCHAR(16) NOT NULL, class_id INT NOT NULL); INSERT INTO student VALUES(1, "qf", 3), (2, "lap", 3), (3, "qfa", 8) //班级表 CREATE TABLE class( classID INT PRIMARY KEY , className VARCHAR(16) NOT NULL ); INSERT INTO class VALUES (3, "liaoshihua"), (8, "changan")
//以下两行的功能是类似的: 前者用JOIN, 后者用子查询 SELECT s.ID, s.s_name, c.className FROM student s LEFT JOIN class c ON s.class_id = c.classID SELECT s.ID, s.s_name, (SELECT c.className FROM class c WHERE s.class_id = c.classID) AS className FROM student s //这里的AS语句要用在查询的结果之后,修饰查询的结果.
新战场:https://blog.csdn.net/Stephen___Qin