![]() If you want to sort the result set of a union, you use an ORDER BY clause in the last SELECT statement as shown in the following example: SELECT concat(firstName, ' ',lastName) fullname It uses the CONCAT() function to concatenate first name, space, and last name into a full name. ![]() This example uses the column heading of the first query for the output. 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. ![]() 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 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. Because UNION ALL does not need to handle duplicates, it performs faster than UNION DISTINCT . If you use the UNION ALL explicitly, the duplicate rows, if available, remain in the result. The following Venn diagram illustrates the union of two result sets that come from t1 and t2 tables: ![]() The final result set contains the distinct values from separate result sets returned by the queries: + -+Ĥ rows in set ( 0.00 sec) Code language: SQL (Structured Query Language) ( sql )īecause the rows with value 2 and 3 are duplicates, the UNION removed them and kept only unique values. UNION SELECT id FROM t2 Code language: SQL (Structured Query Language) ( sql ) The following statement combines result sets returned from t1 and t2 tables: SELECT id FROM t1 INSERT INTO t2 VALUES ( 2),( 3),( 4) Code language: SQL (Structured Query Language) ( sql ) Let’s see the following sample tables: t1 and t2: DROP TABLE IF EXISTS t1 Second, the data types of columns must be the same or compatible.īy default, the UNION operator removes duplicate rows even if you don’t specify the DISTINCT operator explicitly.First, the number and the orders of columns that appear in all SELECT statements must be the same.To combine result set of two or more queries using the UNION operator, these are the basic rules that you must follow: Code language: SQL (Structured Query Language) ( sql ) The following illustrates the syntax of the UNION operator: SELECT column_list MySQL UNION operator allows you to combine two or more result sets of queries into a single result set. The fact that I didn’t use the alias in the ORDER BY clause was enough to produce the error.Summary : in this tutorial, you will learn how to use MySQL UNION operator to combine two or more result sets from multiple SELECT statements into a single result set. Here I tried to order the results by the TeacherName column, but that column has an alias. Result: ERROR 1054 (42S22): Unknown column 'TeacherName' in 'order clause' Here’s an example of code that produces the error: (SELECT TeacherName t FROM Teachers) Alternatively, you could remove the alias and reference the column name directly. So to fix the error, be sure to reference the column by its alias. When using the UNION clause in MySQL, if a column has an alias, then you must use that alias in any ORDER BY clause that references that column. If you’re getting error “1054 (42S22): Unknown column ‘…’ in ‘order clause'” when using the UNION clause in MySQL, it could be because you’re trying to reference an aliased column by its column name.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |