troubleshooting Question

In MySQL, how do I subtract the column values between 2 queries all in a single query?

Avatar of seaero
seaero asked on
MySQL Server
9 Comments1 Solution558 ViewsLast Modified:
I have 2 separate queries that return identically structured tables. I will need to combine the two tables such that the result table containes the difference between a column in both tables. In other words:

Query1:  (Select id1,qty1 from TableA Left Join TableB)

id1    qty1
1      10
2      20
3      8
4      5
5      3

Query2: (Select id2,qty2 from TableC Left Join tableD)

id2    qty2
1      3
2      16
3      2
4      0
6      1

Result: (Query1 - Query2)
1      7
2      4
3      6
4      5
5      3

As the result indicates, I will need to retain all ids returned from Query 1 but not Query 2.

Muhammad Khan
Manager, IT

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros