An example is provided to illustrate the function of union all in the database. The present table Aid name1 name21 aa bb2 aa cc3 bb cc4 aa dd ................. www.2cto.com needs to count the number of times each name appears. The expected result is aa 3bb 2cc 2dd 1. The following is a specific step. The first step of reflecting the role of union all through step-by-step analysis is as follows: query the data information of name1 www.2cto.com select name1 name, count (*) num from dd group by name1: name num aa 3 bb 1 ******************************* Step 2: query the data information of name2: select name2 name, count (*) num from dd group by name2: name num bb 1 cc 2 dd 1 ******************************** ** Step 3: combine the first and second steps with the union all results www.2cto.com select name1 name, count (*) num from dd group by name1union allselect name2 name, count (*) num from dd group by name2 query result: name num aa 3bb 1bb 1cc 2dd 1 ******************************** * ******** search again based on Step 3 to obtain the result select name, sum (num) from (select name1 name, count (*) num from dd group by name1 union all select name2 name, count (*) num from dd group by name2) t www.2cto.com group by name; query result: name num aa 3bb 2cc 2 dd 1