先附上数据。
CREATE TABLE `course` ( `cno` int(11) NOT NULL, `cname` char(30) CHARACTER SET utf8 NOT NULL, `ctime` int(11) NOT NULL, `scount` int(11) NOT NULL, `ctest` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `course` VALUES ('4', '应用数学基础', '48', '120', '2016-03-10 10:08:29'); INSERT INTO `course` VALUES ('5', '生物工程概论', '32', '80', '2016-03-10 10:09:24'); INSERT INTO `course` VALUES ('1', '计算机软件基础', '32', '70', '2016-03-10 10:09:47'); INSERT INTO `course` VALUES ('2', '计算机硬件基础', '24', '80', '2016-03-10 10:10:28'); INSERT INTO `course` VALUES ('8', '模拟电路设计', '28', '90', '2016-04-06 10:11:02'); INSERT INTO `course` VALUES ('7', '机械设计实践', '48', '68', '2016-03-10 10:11:29'); INSERT INTO `course` VALUES ('3', '生物化学', '32', '40', '2016-03-29 10:11:54'); INSERT INTO `course` VALUES ('9', '数据库设计', '16', '80', '2016-03-10 10:12:14'); INSERT INTO `course` VALUES ('6', '设计理论', '28', '45', '2016-03-10 10:12:33'); INSERT INTO `course` VALUES ('10', '计算机入门', '24', '150', '2016-03-10 10:12:53'); INSERT INTO `course` VALUES ('11', '数字电路设计基础', '30', '125', '2016-03-10 10:13:10'); CREATE TABLE `student` ( `sno` char(4) CHARACTER SET utf8 DEFAULT NULL, `sname` char(10) CHARACTER SET utf8 DEFAULT NULL, `dname` char(10) CHARACTER SET utf8 DEFAULT NULL, `ssex` char(2) CHARACTER SET utf8 NOT NULL, `cno` int(11) NOT NULL, `mark` decimal(3,1) NOT NULL, `type` char(4) CHARACTER SET utf8 NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `student` VALUES ('9701', '刘建国', '管理工程', '男', '4', '82.5', '必修'); INSERT INTO `student` VALUES ('9701', '刘建国', '管理工程', '男', '10', '70.0', '必修'); INSERT INTO `student` VALUES ('9701', '刘建国', '管理工程', '男', '1', '78.5', '选修'); INSERT INTO `student` VALUES ('9702', '李春', '环境工程', '女', '5', '63.0', '必修'); INSERT INTO `student` VALUES ('9702', '李春', '环境工程', '女', '10', '58.0', '选修'); INSERT INTO `student` VALUES ('9703', '王天', '生物', '男', '5', '48.5', '必修'); INSERT INTO `student` VALUES ('9703', '王天', '生物', '男', '2', '86.0', '选修'); INSERT INTO `student` VALUES ('9704', '李华', '计算机', '女', '4', '76.0', '必修'); INSERT INTO `student` VALUES ('9704', '李华', '计算机', '女', '1', '92.0', '必修'); INSERT INTO `student` VALUES ('9704', '李华', '计算机', '女', '2', '89.0', '必修'); INSERT INTO `student` VALUES ('9704', '李华', '计算机', '女', '9', '80.0', '必修'); INSERT INTO `student` VALUES ('9704', '李华', '计算机', '女', '8', '70.0', '选修'); INSERT INTO `student` VALUES ('9705', '孙庆', '电子工程', '男', '8', '79.0', '必修'); INSERT INTO `student` VALUES ('9705', '孙庆', '电子工程', '男', '1', '59.0', '必修'); INSERT INTO `student` VALUES ('9705', '孙庆', '电子工程', '男', '11', '52.0', '必修'); INSERT INTO `student` VALUES ('9705', '孙庆', '电子工程', '男', '6', '68.0', '必修'); INSERT INTO `student` VALUES ('9706', '高伟', '机械工程', '男', '13', '93.0', '必修'); INSERT INTO `student` VALUES ('9706', '高伟', '机械工程', '男', '12', '88.5', '必修'); INSERT INTO `student` VALUES ('9706', '高伟', '机械工程', '男', '1', '78.0', '选修'); INSERT INTO `student` VALUES ('9706', '高伟', '机械工程', '男', '10', '76.0', '选修'); CREATE TABLE `teacher` ( `tno` int(11) NOT NULL, `tname` varchar(10) CHARACTER SET utf8 NOT NULL, `cno` int(11) NOT NULL, `sal` int(11) DEFAULT NULL, `dname` char(10) CHARACTER SET utf8 NOT NULL, `tsex` char(2) CHARACTER SET utf8 NOT NULL, `age` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `teacher` VALUES ('1', '王军', '4', '800', '数学', '男', '32'); INSERT INTO `teacher` VALUES ('2', '李丹', '5', '1200', '生物', '女', '54'); INSERT INTO `teacher` VALUES ('3', '王永军', '1', '900', '计算机', '男', '40'); INSERT INTO `teacher` VALUES ('4', '刘小静', '2', '1200', '计算机', '女', '46'); INSERT INTO `teacher` VALUES ('5', '高伟', '8', '2100', '电子工程', '男', '39'); INSERT INTO `teacher` VALUES ('6', '李伟', '7', '1200', '机械工程', '男', '29'); INSERT INTO `teacher` VALUES ('7', '刘辉', '3', '900', '生物', '女', '46'); INSERT INTO `teacher` VALUES ('8', '刘静', '12', '1300', '经济管理', '女', '28'); INSERT INTO `teacher` VALUES ('9', '李伟', '9', null, '计算机', '女', '43'); INSERT INTO `teacher` VALUES ('10', '刘一凯', '13', null, '计算机', '女', '33');
简单的二表连接
SELECT tname,dname,cname,ctest from teacher,course WHERE teacher.cno=course.cno
SELECT tname,dname,cname,ctest from teacher,course
SELECT COLUMN from join_table join_type join_table on (join_condition)
SELECT s.sname,s.dname,s.cno,s.mark from student s where s.mark<60
SELECT s.sname,s.dname,s.cno,s.mark from student s where s.sno in(SELECT DISTINCT s.sno from student s where s.mark<60)
SELECT DISTINCT s.sname,s.dname,s.cno,s.mark from student s,student s2 where s.sno=s2.sno and s2.mark<60
SELECT s.sname,s.dname,s.cno,s.mark from student s,student s2 where s.sno=s2.sno and s2.mark<60
SELECT DISTINCT s.sname,s.dname,s.cno,s.mark from student s,student s2 where s.sno=s2.sno and s.mark<60
SELECT sname,dname,cno,tname from student NATURAL join teacher
SELECT sname,s.dname,s.cno,tname from student s, teacher t where s.dname=t.dname and s.cno=t.cno
SELECT s.sno,sname,s.cno,cname,ctest,mark from student s LEFT JOIN course c on s.cno=c.cno ORDER BY sname
SELECT s.sno,sname,s.cno,cname,ctest,mark from student s full OUTER JOIN course c on s.cno=c.cno ORDER BY sname