![]() If you use the UNION ALL explicitly, the duplicate rows, if available, remain in the result. Because UNION ALL does not need to handle duplicates, it performs faster than UNION DISTINCT . UNION ALL SELECT id FROM t2 Code language: SQL (Structured Query Language) ( sql ) + -+Ħ rows in set ( 0.00 sec) Code language: SQL (Structured Query Language) ( sql )Īs you can see, the duplicates appear in the combined result set because of the UNION ALL operation. We’ll use the customers and employees tables in the sample database for the demonstration: The following picture illustrates the difference between UNION and JOIN: MySQL UNION and column alias examples JOINĪ JOIN combines result sets horizontally, a UNION appends result set vertically. ![]() UNION SELECT CONCAT(contactFirstName, ' ',contactLastName) If you want to use other column headings, you need to use column aliases explicitly in the first SELECT statement as shown in the following example: SELECT CONCAT(firstName, ' ',lastName) fullname Suppose that you want to combine the first name and last name of employees and customers into a single result set, you can use the UNION operator as follows: SELECTĬustomers Code language: SQL (Structured Query Language) ( sql )Īs you can see from the output, the MySQL UNION uses the column names of the first SELECT statement for the column headings of the output. ![]() This example uses the column heading of the first query for the output. It uses the CONCAT() function to concatenate first name, space, and last name into a full name.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |