|
Custom Search
| |
|
How to Simulate Full Join in MySQL - Part 3: use UNION to simulate FULL JOIN Because MySQL does not support Full Join, in Part 1 and Part 2, we have looked at how to simulate Full Join in MySQL. In this tutorial, we'll demonstrate another technique - how to simulate Full Join by using UNION only. First, we need to create our sample tables and then populate them with data. The scenario of this exercise is that our employees generate sales both offline in the stores and online via various online marketing mechanisms. -- Sales by employee in stores create table store_sales ( EmployeeName varchar(20) NOT NULL, Sales double NOT NULL ); -- Sales by employee on the internet create table online_sales ( EmployeeName varchar(20) NOT NULL, Sales double NOT NULL ); -- Add sales data for store sales insert into store_sales values('Joe',516.30), ('Gary',1136.32), ('Jill',589.54), ('Ted',986.54), ('Bob',656.66), ('Mick',887.36), ('Pam',1258.13); -- Add sales data for online sales insert into online_sales values('Sam',784.35), ('Gary',1589.22), ('Jeff',223.12), ('Martin',1456.99), ('Mick',2435.78), ('Jay',565.4), ('Jill',600.23); We are given a task to generate a report with 3 columns where it lists employee names, their total sales by store, and totals sales online. Because some employees only do stores or online sales and some of them do both, our report needs to aggregate all the sales for them. If an employee does only store sales, his/her online sales will display 0, and vice versa. The inner query below uses UNION to combine store sales with online sales, so it vertically appends the two types of sales. Note that for records from store sales table, an online sales column is created with value 0 assigned. For records from online sales table, a store sales column is created with value 0 assigned. The outer query aggregates the two types of sales so for those employees who work for both store and online, their sales are shown side-by-side with the actual total sales listed. select EmployeeName, sum(StoreSales) as StoreSalesTotal, sum(OnlineSales) as OnlineSalesTotal from ( select EmployeeName, Sales as StoreSales, 0 as OnlineSales from store_sales UNION select EmployeeName, 0 as "StoreSales", Sales as "OnlineSales" from online_sales ) as x group by EmployeeName order by EmployeeName;
11 records returned:
If we're able to use FULL JOIN in MySQL, the code would be looking like below if we want it to return an identical result as above.
Happy Coding! Other tutorials in this category 1. Using Inner Joins to Combine Data from Two Tables |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |