如何结合两个mysql查询结果?

时间:2011-12-23 05:09:05

标签: mysql

QUERY 1:

SELECT SUM(total) AS total, company, id 
FROM
(SELECT  invoicereferencenumber AS ref, invoiceamount AS total, companyname AS company, customer.customerid AS id FROM customer, rental_invoice AS r WHERE customer.customerid=r.customerid
UNION
SELECT  invoicereferencenumber AS ref, invoiceamount AS total, companyname AS company, customer.customerid AS id FROM customer, sale_invoice AS s WHERE customer.customerid=s.customerid
UNION
SELECT  invoicereferencenumber AS ref, invoiceamount AS total, companyname AS company, customer.customerid AS id FROM customer, service_invoice AS se WHERE customer.customerid=se.customerid)
AS tmptotal
GROUP BY id

QUERY 2:

SELECT SUM(paid) AS paid, company, cid 
FROM
(SELECT cash_amount AS paid, companyname AS company, customerid AS cid FROM payment_cash AS p, customer AS c WHERE cash_cus_id=customerid
UNION
SELECT cheque_value AS paid, companyname AS company, customerid AS cid FROM payment_cheque AS p, customer AS c WHERE cheque_cus_id=customerid
UNION
SELECT rtgs_value AS pain, companyname AS company, customerid AS cid FROM payment_rtgs AS p, customer AS c WHERE rtgs_cus_id=customerid)
AS tmppaid
GROUP BY cid

我必须将这些结果合并为

----------------------------------------
|  total  |   Paid   |  company  | id  |
----------------------------------------

请帮帮我。 提前致谢

2 个答案:

答案 0 :(得分:1)

好吧,我会先尝试像

这样的东西
SELECT  TotalQuery.*,
        PaidQuery.paid
FROM    (
            SELECT SUM(total) AS total, company, id  
            FROM 
            (SELECT  invoicereferencenumber AS ref, invoiceamount AS total, companyname AS company, customer.customerid AS id FROM customer, rental_invoice AS r WHERE customer.customerid=r.customerid 
            UNION 
            SELECT  invoicereferencenumber AS ref, invoiceamount AS total, companyname AS company, customer.customerid AS id FROM customer, sale_invoice AS s WHERE customer.customerid=s.customerid 
            UNION 
            SELECT  invoicereferencenumber AS ref, invoiceamount AS total, companyname AS company, customer.customerid AS id FROM customer, service_invoice AS se WHERE customer.customerid=se.customerid) 
            AS tmptotal 
            GROUP BY id
        ) TotalQuery LEFT JOIN
        (
            SELECT SUM(paid) AS paid, company, cid  
            FROM 
            (SELECT cash_amount AS paid, companyname AS company, customerid AS cid FROM payment_cash AS p, customer AS c WHERE cash_cus_id=customerid 
            UNION 
            SELECT cheque_value AS paid, companyname AS company, customerid AS cid FROM payment_cheque AS p, customer AS c WHERE cheque_cus_id=customerid 
            UNION 
            SELECT rtgs_value AS pain, companyname AS company, customerid AS cid FROM payment_rtgs AS p, customer AS c WHERE rtgs_cus_id=customerid) 
            AS tmppaid 
            GROUP BY cid 
        ) PaidQuery     ON  TotalQuery.id = PadiQuery.id
                        AND TotalQuery.company = PaidQuery.company

假设您必须拥有一笔总额,以便您支付了一些费用,这将返回所有copany,id分组的列表及其总数和支付金额。

答案 1 :(得分:0)

您可以通过加入所有表来组合这两个查询:

Select Sum(ri.invoicereferencenumber) + 
       Sum(si.invoicereferencenumber) +
       SUM(se.invoicereferencenumber) as Total,
       SUM(pc.cash_amount) + SUM(pch.cheque_value) + SUM(pr.rtgs_value) as Paid,
       c.Company, c.Id
from customer c
inner join rental_invoice  ri  on c.customerid = ri.customerid
inner join sale_invoice    si  on c.customerid = si.customerid
inner join service_invoice se  on c.customerid = se.customerid
inner join payment_cash    pc  on c.customerid = pc.cash_cus_id
inner join payment_cheque  pch on c.customerid = pch.cheque_cus_id
inner join payment_rtgs    pr  on c.customerid = pr.rtgs_cus_id
group by c.Company, c.Id