Asfi Tanoli Asfi Tanoli - 3 months ago 11
MySQL Question

How to Join two big Queries?

Here is my first query..

SELECT cmp.bundles_id,
MONTH(cmp.payment_date) as month,
sum(vb.bundle_price) as bundle_Price
FROM `voice_bundles` as cmp
join `voice_bundles_price` as vb
ON cmp.bundles_id=vb.id
GROUP BY MONTH(cmp.payment_date)


Here is my 2nd query..

SELECT
MONTH(bill_due_date) as month,
sum(charges) as Intercom,
sum(package_rent) as packages_price
from `bill`
GROUP BY MONTH(bill_due_date)


I want to show the data in the same table according to the month... but the problem is field name which is
MONTH(bill_due_date) as month
and
MONTH(cmp.payment_date) as month


Months will be Same for both queries..

How do i make combine both queries and make it one.. and want the output like this?

enter image description here

Answer

You can try a join between the table

select a.month, a.bundle_Price,  b.Intercom, b.packages_price
from (
  SELECT cmp.bundles_id, 
  MONTH(cmp.payment_date) as month, 
  sum(vb.bundle_price) as bundle_Price 
  FROM `voice_bundles` as cmp 
  join `voice_bundles_price` as vb 
  ON cmp.bundles_id=vb.id 
  GROUP BY MONTH(cmp.payment_date) 7
) a
inner join (
  SELECT 
  MONTH(bill_due_date) as month, 
  sum(charges) as Intercom,
  sum(package_rent) as packages_price 
  from `bill` 
  GROUP BY MONTH(bill_due_date)
) b
on a.month = b.month
Comments