当前位置:网站首页>Non duplicate data values of two MySQL query tables
Non duplicate data values of two MySQL query tables
2022-04-23 08:50:00 【Tag heartbeat】
List of articles
Preface
In data migration , We often need to compare two tables , In order to identify records in one table that do not have corresponding records in another table .
for example , We have a new database , Its architecture is different from the old database . Our task is to migrate all data from the old database to the new database , And verify that the data is migrated correctly . To check the data , We have to compare the two tables , One in the new database , One in the old database , And identify mismatched records .
So let's use union all Link two tables , After that, use the temporary table or derived table to compare the data ~
One 、 preparation
Create table :t1
DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
`tid` tinyint(4) NOT NULL AUTO_INCREMENT COMMENT ' Primary key id',
`nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT ' nickname ',
`age` int(11) NULL DEFAULT NULL COMMENT ' Age ',
PRIMARY KEY (`tid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
surface :t1 Insert a piece of data
INSERT INTO `t1` VALUES (1, ' Zhang San ', 18);
Create table :t2
DROP TABLE IF EXISTS `t2`;
CREATE TABLE `t2` (
`tid` tinyint(4) NOT NULL AUTO_INCREMENT,
`nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT ' nickname ',
`age` int(11) NULL DEFAULT NULL COMMENT ' Age ',
`gender` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT ' Gender ',
PRIMARY KEY (`tid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
surface :t2 Insert a piece of data
INSERT INTO `t2` VALUES (1, ' Zhang San ', 18, ' male ');
Be careful : You can see t1、t2 Table attribute values correspond to the same


Two 、 Use tests
SQL:
SELECT
tid,
nickname,
age
FROM
( SELECT tid, nickname, age FROM t1 UNION ALL SELECT tid, nickname, age FROM t2 ) tbl
GROUP BY
tid,
nickname,
age
HAVING
count(*) = 1
ORDER BY
tid;

Of course, there is no data returned after running , Because they are no different . Don't try so hard, , Let's do it again t2 Insert a row of data in the table :
INSERT INTO t2 ( nickname, age, gender )
VALUES
( ' Li Si ', 18, ' male ' );

Re execution SQL( because ‘ Li Si ’ This data , surface t1、t2 If there is no duplication between the two tables of related attribute values, it will be queried ):

版权声明
本文为[Tag heartbeat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230823533453.html
边栏推荐
- 2021 Li Hongyi's adaptive learning rate of machine learning
- IDEA导入commons-logging-1.2.jar包
- Go语言自学系列 | golang嵌套结构体
- 'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
- cadence的工艺角仿真、蒙特卡洛仿真、PSRR
- valgrind和kcachegrind使用運行分析
- 怎样读取Excel表格到数据库
- 论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
- LINQ Learning Series ----- 1.4 anonymous objects
- Go语言自学系列 | golang结构体指针
猜你喜欢

valgrind和kcachegrind使用運行分析

STM32 uses Hal library. The overall structure and function principle are introduced

洋桃电子STM32物联网入门30步笔记四、工程编译和下载

洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置

Star Trek's strong attack opens the dream linkage between metacosmic virtual reality

使用flask和h5搭建网站/应用的简要步骤

idea打包 jar文件

Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard

Consensus Token:web3.0生态流量的超级入口

四张图弄懂matplotlib的一些基本用法
随机推荐
经典题目刷一刷
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
dataBinding中使用include
BK3633 规格书
ESP32程序下载失败,提示超时
valgrind和kcachegrind使用運行分析
L2-024 tribe (25 points) (and check the collection)
Single chip microcomputer nixie tube stopwatch
Complete binary search tree (30 points)
数字政府建设中政务中台中的技术创新点
IDEA导入commons-logging-1.2.jar包
Valgrind and kcache grind use run analysis
synchronized 锁的基本用法
rembg 分割mask
关于cin,scanf和getline,getchar,cin.getline的混合使用
单片机数码管秒表
Automatic differentiation and higher order derivative in deep learning framework
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
【原创】使用System.Text.Json对Json字符串进行格式化
Introduction to GUI programming swing