The difference between UNION and UNION ALL in MySQL is that UNION eliminates duplicate rows, while UNION ALL retains all rows. UNION sorts the result set implicitly, but UNION ALL does not.
The difference between UNION and UNION ALL in MySQL
In MySQL, both UNION and UNION ALL are used Operator that combines row data from different query result sets. However, there are key differences in how the results are processed.
UNION
UNION ALL
Usage scenarios
Example
Suppose there are two tables: table1
and table2
, they both have a table named Column of name
.
The following query uses UNION to merge the rows in the two tables and eliminate duplicate names:
<code class="sql">SELECT name FROM table1 UNION SELECT name FROM table2;</code>
The following query uses UNION ALL to merge the rows in the two tables and retain duplicate names Name:
<code class="sql">SELECT name FROM table1 UNION ALL SELECT name FROM table2;</code>
The above is the detailed content of The difference between union and union all in mysql. For more information, please follow other related articles on the PHP Chinese website!