Union is a union operation on the data, excluding duplicate rows and performing default sorting. Union all is a union operation on the data, including duplicate rows and not sorting. For example: Create the database tables: CREATE TABLE `t_demo` ( `id` int(32) NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int(2) DEFAULT NULL, `num` int(3) DEFAULT NULL, PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=utf8; Copy this table: CREATE TABLE `t_demo_copy` ( `id` int(32) NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int(2) DEFAULT NULL, `num` int(3) DEFAULT NULL, PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=utf8; Add data: INSERT INTO `t_demo` VALUES ('1', '张三', '21', '69'); INSERT INTO `t_demo` VALUES ('2', 'Li Si', '22', '98'); INSERT INTO `t_demo` VALUES ('3', '王五', '20', '54'); INSERT INTO `t_demo` VALUES ('4', '赵甜', '22', '80'); INSERT INTO `t_demo_copy` VALUES ('1', '张三', '21', '69'); INSERT INTO `t_demo_copy` VALUES ('2', 'Zhu Bajie', '22', '98'); INSERT INTO `t_demo_copy` VALUES ('3', '王五', '20', '54'); INSERT INTO `t_demo_copy` VALUES ('4', '赵甜', '22', '80'); INSERT INTO `t_demo_copy` VALUES ('5', '孙武空', '22', '100'); INSERT INTO `t_demo_copy` VALUES ('6', 'Li Si', '24', '99'); UNION in MySQL SELECT * FROM t_demo UNION SELECT * FROM t_demo_copy Query results:
From the above query data we can find that: After UNION links the tables, it will filter out duplicate records, sort the generated result set, delete duplicate records, and then return the results. UNION ALL in MySQL SELECT * FROM t_demo UNION ALL SELECT * FROM t_demo_copy Query results:
From the above data we can see: UNION ALL simply combines the two results and returns them. If there is duplicate data in the two returned result sets, the returned result set will contain the duplicate data. efficiency: In terms of efficiency, UNION ALL is much faster than UNION. Therefore, if you can confirm that the two merged result sets do not contain duplicate data and do not need to be sorted, then use UNION ALL. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Example of how to set up a Linux system to automatically run a script at startup
>>: Summary of using the reduce() method in JS
Table of contents Preface Introduction to Bezier ...
Today I made a menu button. When you move the mous...
The JavaScript hasOwnProperty() method is the pro...
Html event list General Events: onClick HTML: Mous...
The code looks like this: <!DOCTYPE html> &...
When learning mybatis, I encountered an error, th...
Some web pages may not look large but may be very ...
【Introduction】: Handtrack.js is a prototype libra...
Code <div class="test"> <div&g...
Table of contents 1. What is grub encryption 2. g...
Table of contents What is NULL Two kinds of NULL ...
Prerequisite: You need to compile the ngx_http_he...
I installed node to the D drive, and I also neede...
Table of contents 1. Solution 2. MySQL character ...
Table of contents 1. Rename table method 2. Notes...