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

数据库左连接、右连接、全联接、左外、右外、全外

时间:2019-06-14 00:59:29      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:not   col   for   outer   ODB   支持   records   null   nod   

技术图片

内联

技术图片
技术图片
SELECT
*
FROM
temployee employees0
INNER JOIN tcustomer customer1 ON ( customer1.id = employees0.id );

左联

技术图片
技术图片
SELECT
*
FROM
temployee employees0
LEFT OUTER JOIN tcustomer customer1 ON ( customer1.id = employees0.id );

右联

技术图片
技术图片
SELECT
*
FROM
temployee employees0
RIGHT OUTER JOIN tcustomer customer1 ON ( customer1.id = employees0.id );

全联(MySql不支持)

技术图片
SELECT * FROM t_employee te FULL JOIN t_customer tc ON (te.id = tc.id);

左外

技术图片
技术图片
SELECT
*
FROM
temployee employees0
LEFT OUTER JOIN tcustomer customer1 ON ( customer1.id = employees0.id )
WHERE
customer1_.id IS NULL;

右外

技术图片
技术图片
SELECT *
FROM
temployee employees0
RIGHT OUTER JOIN tcustomer customer1 ON ( customer1.id = employees0.id )
WHERE
employees0_.id IS NULL;

全外(MySql不支持)

技术图片
SELECT *
FROM
t_employee te
FULL JOIN t_customer tc ON ( te.id = tc.id )
WHERE
te.id IS NULL
OR tc.id IS NULL;

数据库

Employee
技术图片
Customer
技术图片

-- ----------------------------
-- Table structure for t_employee
-- ----------------------------
DROP TABLE IF EXISTS `t_employee`;
CREATE TABLE `t_employee` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `employee_name` varchar(255) DEFAULT NULL,
  `employee_part` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of t_employee
-- ----------------------------
INSERT INTO `t_employee` VALUES (1, ‘老潘‘, ‘总裁部‘);
INSERT INTO `t_employee` VALUES (2, ‘老王‘, ‘秘书部‘);
INSERT INTO `t_employee` VALUES (3, ‘老张‘, ‘设计部‘);
INSERT INTO `t_employee` VALUES (4, ‘老李‘, ‘运营部‘);

-- ----------------------------
-- Table structure for t_customer
-- ----------------------------
DROP TABLE IF EXISTS `t_customer`;
CREATE TABLE `t_customer` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `customer_name` varchar(255) DEFAULT NULL,
  `customer_part` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of t_customer
-- ----------------------------
INSERT INTO `t_customer` VALUES (2, ‘老王‘, ‘秘书部‘);
INSERT INTO `t_customer` VALUES (3, ‘老张‘, ‘设计部‘);
INSERT INTO `t_customer` VALUES (4, ‘老刘‘, ‘人事部‘);
INSERT INTO `t_customer` VALUES (5, ‘老黄‘, ‘生产部‘);

数据库左连接、右连接、全联接、左外、右外、全外

标签:not   col   for   outer   ODB   支持   records   null   nod   

原文地址:https://blog.51cto.com/12012821/2408733

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