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

Summary of the advantages of Vue3 vs. Vue2

Table of contents 1. Why do we need vue3? 2. Adva...

Example of creating a virtual host based on Apache port

apache: create virtual host based on port Take cr...

10 Tips for Mobile App User Interface Design

Tip 1: Stay focused The best mobile apps focus on...

Summary of several APIs or tips in HTML5 that cannot be missed

In previous blog posts, I have been focusing on so...

Display mode of elements in CSS

In CSS, element tags are divided into two categor...

MySql 5.7.21 free installation version configuration method under win10

1. Unzip to the location where you want to instal...

Complete example of Vue encapsulating the global toast component

Table of contents Preface 1. With vue-cli 1. Defi...

Basic knowledge of HTML: a preliminary understanding of web pages

HTML is the abbreviation of Hypertext Markup Langu...

HTML table tag tutorial (32): cell horizontal alignment attribute ALIGN

In the horizontal direction, you can set the cell...

Steps to install MySQL on Windows using a compressed archive file

Recently, I need to do a small verification exper...

How to safely shut down a MySQL instance

This article analyzes the process of shutting dow...

Detailed tutorial on deploying apollo with docker

1. Introduction I won’t go into details about apo...