Using the UNION Operatorunion : 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. DemoFriends, 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:
|
>>: CocosCreator Typescript makes Tetris game
Table of contents 1. Introduction 2. Recursion 3....
1. First, let’s have a general introduction to th...
The command line mysqld –skip-grant-tables cannot...
The default ssh port number of Linux servers is g...
background All of the company's servers are p...
In the previous article - The charm of one line o...
First, take a look at Alibaba Cloud's officia...
Table of contents Where is the source code of the...
How to save and exit after editing a file in Linu...
When faced with a SQL statement that is not optim...
Copy code The code is as follows: <iframe id=&...
Table of contents 1. Images 1. What is a mirror? ...
Example Usage Copy code The code is as follows: &l...
Suppose Taobao encourages people to shop during D...
1. Install mysql: udo apt-get install mysql-serve...