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

Troubleshooting the reasons why MySQL deleted records do not take effect

A record of an online MySQL transaction problem L...

Calling the search engine in the page takes Baidu as an example

Today, it suddenly occurred to me that it would be...

Vue parent component calls child component function implementation

Vue parent component calls the function of the ch...

Solution to multiple 302 responses in nginx proxy (nginx Follow 302)

Proxying multiple 302s with proxy_intercept_error...

Click the toggle button in Vue to enable the button and then disable it

The implementation method is divided into three s...

In-depth analysis of HTML semantics and its related front-end frameworks

About semantics Semantics is the study of the rel...

Specific usage of CSS compound selectors

Intersection Selector The intersection selector i...

Vue realizes the progress bar change effect

This article uses Vue to simply implement the cha...

JavaScript implements an input box component

This article example shares the specific code for...

How to implement scheduled backup of CentOS MySQL database

The following script is used for scheduled backup...

How to block IP and IP range in Nginx

Written in front Nginx is not just a reverse prox...

HTML basic summary recommendation (title)

HTML: Title Heading is defined by tags such as &l...