使用列计算将查询加入另一个查询

时间:2015-01-23 03:42:57

标签: php mysql sql-server

我有三个名为issue_details,nature_payments和rci_records的表。现在我有了这个连接这三个表的查询。

SELECT  issue_details.issue_date AS Date,
        issue_details.check_no AS Check_No,
        payees.payee_name AS Name_payee,
        nature_payments.nature_payment AS Nature_of_Payment,
        issue_details.issue_amount AS Checks_issued,
        issue_details.nca_balance AS Nca_balance

FROM    
    issue_details

    INNER JOIN
       nature_payments ON
       issue_details.nature_id = nature_payments.nature_id
    INNER JOIN
       payees ON
       issue_details.payee_id = payees.payee_id
       ORDER BY Date Asc, Check_no ASC

在我的Nca_balance列中,这是每次检查发行的计算差异。但你可能不知道我是如何得到差异的过程,但为了简单起见,让我们说我有另一个问题 动态获取此nca_balance列的差异。这是查询:

SELECT r.*,
 (@tot := @tot - issue_amount) as bank_balance
 FROM (SELECT @tot := SUM(nca_amount) as nca_total FROM nca
    WHERE account_type = 'DBP-TRUST' AND 
    year(issue_date) = year('2015-01-11') AND
    month(issue_date) = month('2015-01-11')
  )
vars CROSS JOIN issue_details r
WHERE r.account_type = 'DBP-TRUST' AND
r.issue_date = '2015-01-11'
ORDER BY r.issue_date, r.check_no

我知道你可能不明白我的观点,但我只想替换第一行

issue_details.nca_balance AS Nca_balance

我自己的第二次查询计算。

请帮我把这两个查询合并为一个查询。感谢

0 个答案:

没有答案