MYSQL uses Union to merge the data of two tables and display them

MYSQL uses Union to merge the data of two tables and display them

Using the UNION Operator

union : Used to connect the results of two or more SELECT statements into one result set. Multiple SELECT statements will remove duplicate data.

Using the union operator will retrieve the same data from multiple tables once. If you want to display the values ​​in Table 1 and Table 2 completely, you can use union all.

Demo

Friends, create a table by yourself.

The data in Table 1 are as follows:

The data in Table 2 are as follows:

OK, the table data has been created, with a total of five records. Next, let's take a look at the use of union and union all.

Use union to see the effect:

select t1.id id, t1.name name, t1.description description,t1.create_time time from table1 t1
UNION
select t2.id id, t2.name name, t2.description description,t2.create_date time from table2 t2 

We can see that using union will only find four pieces of data. If two of them are identical data, only one will be displayed.

Use union all to see the effect:

select t1.id id, t1.name name, t1.description description,t1.create_time time from table1 t1
UNION ALL
select t2.id id, t2.name name, t2.description description,t2.create_date time from table2 t2 

Use union all to find out 5 pieces of data. PS: The same data will also be queried.

expand:

In order to distinguish which table the data is in, we can do this

select t1.id id, t1.name name, t1.description description,t1.create_time time,'table1' type from table1 t1
UNION ALL
select t2.id id, t2.name name, t2.description description,t2.create_date time,'table2' type from table2 t2 

Sort the data in the two tables by time

select t3.* from (select t1.id id, t1.name name, t1.description description,t1.create_time time,'table1' type from table1 t1
UNION ALL
select t2.id id, t2.name name, t2.description description,t2.create_date time,'table2' type from table2 t2) t3 order by t3.time desc 

This is the end of this article about how to use Union in MYSQL to merge and display data from two tables. For more relevant MySQL data merging and display content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL union syntax code example analysis
  • Study on the default rules of mySQL UNION operator
  • How to use union all in MySQL to get the union sort
  • Basic usage of UNION and UNION ALL in MySQL
  • Brief analysis of MySQL union and union all
  • How to merge multiple rows of data into one row in mysql

<<:  MySQL8.0 installation process under Centos7 in VMware workstation16 and Navicat remote connection

>>:  CocosCreator Typescript makes Tetris game

Recommend

Share 5 JS high-order functions

Table of contents 1. Introduction 2. Recursion 3....

Linux type version memory disk query command introduction

1. First, let’s have a general introduction to th...

CentOS method to modify the default ssh port number example

The default ssh port number of Linux servers is g...

Can CSS be used like this? The art of whimsical gradients

In the previous article - The charm of one line o...

Processing ideas for decrypting WeChat applet packages on PC in node.js

Table of contents Where is the source code of the...

Practical explanation of editing files, saving and exiting in linux

How to save and exit after editing a file in Linu...

Tips for optimizing MySQL SQL statements

When faced with a SQL statement that is not optim...

How to point the target link of a tag to iframe

Copy code The code is as follows: <iframe id=&...

HTML meta usage examples

Example Usage Copy code The code is as follows: &l...

Use of MySQL DATE_FORMAT function

Suppose Taobao encourages people to shop during D...