码迷,mamicode.com
首页 > 数据库 > 详细

mysql INNER/LEFT/RIGHT JOIN区别

时间:2019-01-17 19:36:17      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:inner   分享图片   src   create   info   bubuko   sts   分享   utf8   

 1、创建table

DROP TABLE IF EXISTS `tab_id_index`;
CREATE TABLE `tab_id_index` (
  `id` int(11) NOT NULL DEFAULT 0,
  `name` varchar(10) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of tab_id_index
-- ----------------------------
INSERT INTO `tab_id_index` VALUES (1, 3, 12);
INSERT INTO `tab_id_index` VALUES (2, 3, 12);
INSERT INTO `tab_id_index` VALUES (3, 3, 12);
INSERT INTO `tab_id_index` VALUES (5, 4, 14);
DROP TABLE IF EXISTS `tab_no_index`;
CREATE TABLE `tab_no_index` (
  `id` int(11) NOT NULL DEFAULT 0,
  `name` varchar(10) DEFAULT NULL,
  `age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of tab_no_index
-- ----------------------------
INSERT INTO `tab_no_index` VALUES (1, 1, 12);
INSERT INTO `tab_no_index` VALUES (2, 1, 12);
INSERT INTO `tab_no_index` VALUES (3, 3, 12);
INSERT INTO `tab_no_index` VALUES (4, 6, 14);

 

2、sql查询

select * from tab_id_index a,tab_no_index b where a.id = b.id;

同:

select * from tab_id_index a INNER JOIN tab_no_index b ON a.id = b.id;

技术分享图片

 


select * from tab_id_index a LEFT JOIN tab_no_index b ON a.id = b.id;

同:

select * from tab_id_index a LEFT OUTER JOIN tab_no_index b ON a.id = b.id;

技术分享图片

 


select * from tab_id_index a RIGHT JOIN tab_no_index b ON a.id = b.id;

同:

select * from tab_id_index a RIGHT OUTER JOIN tab_no_index b ON a.id = b.id;

技术分享图片

 

mysql INNER/LEFT/RIGHT JOIN区别

标签:inner   分享图片   src   create   info   bubuko   sts   分享   utf8   

原文地址:https://www.cnblogs.com/bestzhang/p/10283660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!