• UNION is more commonly used. UNION all is a direct connection, so all values are taken, and there may be duplication in the record. UNION is a unique value, and there is no duplication in the record

    1. The syntax of UNION is as follows:

    [SQL statement 1] UNION [SQL statement 2]Copy the code

    2, UNION ALL

    [SQL statement 1] UNION ALL [SQL statement 2]Copy the code
  • Efficiency: The UNION and UNION ALL keywords combine two result sets into one, but they differ in usage and efficiency.

    1. Processing of duplicate results: UNION will filter out duplicate records after table linking, and UNION All will not remove duplicate records.

    2. Sorting: The Union will sort by the order of the fields; The UNION ALL simply merges the two results and returns.

  • A UNION ALL is much faster than a UNION in terms of efficiency, so use a UNION ALL if you can be sure that the two result sets that are merged do not contain duplicate data and do not need sorting.

    The UNION is de-duplicated and sorted. The UNION ALL is de-duplicated and sortedCopy the code