MySQL Forums
Forum List  »  InnoDB

Re: Using UNION to add selected field in multiple select statements?
Posted by: Muhammad Omair
Date: February 02, 2016 08:15AM

The below query will add the first two values and subtract the next two. You can replace each sample SQL with your required SQL query.
SELECT sum(col1) + sum(col2) AS addition, sum(col3) - sum(col4) AS subtract FROM (SELECT 1 as col1, 0 AS col2, 0 AS col3, 0 AS col4 UNION ALL SELECT 0 AS col1, 2 AS col2, 0 AS col3, 0 AS col4 UNION ALL SELECT 0 AS col1, 0 AS col2, 3 AS col3, 0 AS col4 UNION ALL SELECT 0 AS col1, 0 AS col2, 0 AS col3, 4 AS col4 ) sub;

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Using UNION to add selected field in multiple select statements?
954
February 02, 2016 08:15AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.