MySql——各類join的區別

select c.cno,c.cname,t.tname from course c ,teacher t where t.tno=c.tno

首先建立表course,sc,teacher表。sql

course表:3d

DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cno` varchar(10) NOT NULL,
  `cname` varchar(20) DEFAULT NULL,
  `tno` varchar(20) NOT NULL,
  PRIMARY KEY (`cno`,`tno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('c001', 'J2SE', 't002');
INSERT INTO `course` VALUES ('c002', 'Java Web', 't002');
INSERT INTO `course` VALUES ('c003', 'SSH', 't001');
INSERT INTO `course` VALUES ('c004', 'Oracle', 't001');
INSERT INTO `course` VALUES ('c005', 'SQL SERVER 2005', 't003');
INSERT INTO `course` VALUES ('c006', 'C#', 't003');
INSERT INTO `course` VALUES ('c007', 'JavaScript', 't002');
INSERT INTO `course` VALUES ('c008', 'DIV+CSS', 't001');
INSERT INTO `course` VALUES ('c009', 'PHP', 't003');
INSERT INTO `course` VALUES ('c010', 'EJB3.0', 't002');

sc表:code

DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
  `sno` varchar(10) NOT NULL,
  `cno` varchar(10) NOT NULL,
  `score` int(11) DEFAULT NULL,
  PRIMARY KEY (`sno`,`cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES ('s001', 'c001', '79');
INSERT INTO `sc` VALUES ('s001', 'c002', '83');
INSERT INTO `sc` VALUES ('s001', 'c003', '59');
INSERT INTO `sc` VALUES ('s002', 'c001', '81');
INSERT INTO `sc` VALUES ('s002', 'c002', '73');
INSERT INTO `sc` VALUES ('s003', 'c001', '82');
INSERT INTO `sc` VALUES ('s003', 'c002', '82');
INSERT INTO `sc` VALUES ('s004', 'c001', '61');

teacher表:blog

DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `tno` varchar(10) NOT NULL,
  `tname` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`tno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('t001', '劉陽');
INSERT INTO `teacher` VALUES ('t002', '諶燕');
INSERT INTO `teacher` VALUES ('t003', '胡明星');

 

        1.inner join(內鏈接或等值鏈接) : 獲取兩個表中字段匹配關係的記錄。ip

        如今咱們打算篩選出課程對應的教師名字,能夠使用如下語句:class

select c.cno,c.cname,t.tname from course c INNER JOIN teacher t on t.tno=c.tno

        能夠獲得如下結果:select

        不過目前使用內鏈接的次數比較少,通常直接使用相同項相等便可。如上條SQL能夠寫爲:im

select c.cno,c.cname,t.tname from course c ,teacher t where t.tno=c.tno

        2.left join(左鏈接) : 獲取左表的全部記錄,即便右表沒有記錄。d3

        如今咱們打算選出每科考試學生的平均成績,能夠使用如下語句:img

SELECT c.cname,sc.avgs from course c LEFT JOIN (select cno,AVG(score) as avgs from sc GROUP BY cno) sc ON sc.cno=c.cno

        運行後得出結果:

        3.right join(右鏈接) : 與左鏈接相反。

            如今,咱們打算看看每一個平均分對應的科目名字,能夠使用如下語句:

SELECT c.cname,sc.avgs from course c RIGHT JOIN (select cno,AVG(score) as avgs from sc GROUP BY cno) sc ON sc.cno=c.cno

            運行後得出結果:

            4.inner Join後不添加任何條件(全鏈接):

                咱們運行如下SQL:

select c.cno,c.cname,t.tname from course c INNER JOIN teacher t

                能夠獲得如下結果:

 

相關文章
相關標籤/搜索