![]() Suppose we have entered some records as samples for the tables Person and Customer with the help of the MySQL query statements below, respectively:Ĭode: INSERT INTO Person('Person_ID', 'Person_Name', 'Person_Address') VALUES ('101','Akash','Delhi') We will perform the following SQL queries to create database tables for demonstrating the examples:Ĭode: CREATE TABLE Person(Person_ID INT PRIMARY KEY AUTO_INCREMENT, Person_Name VARCHAR(255) NOT NULL, Person_Address VARCHAR(255) NOT NULL) ĬREATE TABLE Customer(CustomerID INT PRIMARY KEY AUTO_INCREMENT, CustomerName VARCHAR(255) NOT NULL, Credit_Limit DECIMAL(10,2) NOT NULL, City VARCHAR(255) NOT NULL) First, we will create a table Person with fields Person_ID, Person_Name, and Person_Address and again create a second table Customer with fields CustomerID, CustomerName, Credit_Limit, and City. ![]() Let us consider two tables for implementing the query statements using both operators. Let us evaluate and illustrate some of the examples showing the working and results of the MySQL UNION ALL operator in the database upon the related tables: ![]() In that case, the result rows are retrieved but comprise duplicate row values between the several SELECT statements used in the command to make a union set of database tables. Suppose everything is proper in the UNION ALL query statement. This ensures that the resultant rows do not mismatch and produce any MySQL code error.Therefore, it fetches the rows from both tables where each statement with the SELECT keyword command must include an equal number of column fields and matching data types. We know that the MySQL UNION ALL operator allows combining the result rows of multiple SELECT queries.How does UNION ALL Operator work in MySQL? The number of Column fields must be identical for each SELECT query included, and the statement must consist of at least a single table mentioned with the FROM clause.WHERE condition is an optional matching expression for the SELECT statement fields in the tables provided.The Col_expr1, Col_expr2,…, and Col_exprN are the expressional column values required to be fetched from two or more tables like TableName_A or TableName_B.Here, let us explain the parameters listed above: SELECT Col_expr1, Col_expr2,…,Col_exprN FROM TableName_B The sub-select sort is limited to the scope of that sub-select - the ORDER BY of the UNION will determine the sort of the final result set.Hadoop, Data Science, Statistics & others SELECT Col_expr1, Col_expr2,…,Col_exprN FROM TableName_A ![]() What's more, I am using an ASC sort even though one of the sub-selects is using a DESC sort. That said, you can see that I am using an ORDER BY clause to sort the derived result set and then a LIMIT clause to limit the size of the returned result set. But, it wouldn't matter which one I chose for this demo. ![]() I happen to be using "UNION ALL," instead of "UNION" in this case, because I know that there will be no duplicate rows in the result set the UNION ALL action saves a little bit of processing overhead. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |