mysql INNER/LEFT/RIGHT JOIN区别

Posted love_best

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql INNER/LEFT/RIGHT JOIN区别相关的知识,希望对你有一定的参考价值。

 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区别的主要内容,如果未能解决你的问题,请参考以下文章

MySQL常用语法

Mysql -- 查询语句

MySQL数据表查询操作

MySQL数据表查询操作

MySQL数据表查询操作

R语言dplyr包进行dataframe的连接(inner_joinleft_joinright_joinfull_joinsemi_joinanti_join)操作实战