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
In the previous article, we have realized the sim...
A record of an online MySQL transaction problem L...
Today, it suddenly occurred to me that it would be...
The table structure is as follows. There are only...
Vue parent component calls the function of the ch...
What we are simulating now is a master-slave syst...
Proxying multiple 302s with proxy_intercept_error...
The implementation method is divided into three s...
About semantics Semantics is the study of the rel...
Intersection Selector The intersection selector i...
This article uses Vue to simply implement the cha...
This article example shares the specific code for...
The following script is used for scheduled backup...
Written in front Nginx is not just a reverse prox...
HTML: Title Heading is defined by tags such as &l...