我已经使用PHP MYSQLi创建了资产负债表系统。管理员可以在哪里管理客户帐户信息。现在,我只想获取 DEBTORS (债权人)和 CREDITORS(债权人)。我正在使用action_type
字段'dr'或'cr'管理这两个条目。
下面是表结构:
我尝试了以下查询以仅获取债务人:
SELECT *, client_id
, SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debits
, SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) total_credits
, SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debtors
, SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) balance
FROM tbl_balancesheet
GROUP
BY client_id
HAVING balance <> 0
但是它得到了债务人和债权人的所有结果。
如何使用查询或通过PHP代码仅获得债务人和债权人?
此外,如何在余额列中显示dr和cr?
请帮助!!!
谢谢
答案 0 :(得分:1)
似乎您只需要修改HAVING
子句,以过滤掉balance
大于零(债务人)或balance
小于零(证人)的情况
您还可以使用条件IF()
表达式来确定balance
列中的 dr / cr 。
仅获取债务人:
SELECT client_id,
Sum(Coalesce(CASE
WHEN action_type = 'dr' THEN amount
end, 0)) AS total_debits,
Sum(Coalesce(CASE
WHEN action_type = 'cr' THEN amount
end, 0)) AS total_credits,
Sum(Coalesce(CASE
WHEN action_type = 'cr' THEN amount
end, 0)) - Sum(Coalesce(CASE
WHEN action_type = 'dr' THEN
amount
end, 0)) AS total_debtors,
IF(Sum(Coalesce(CASE
WHEN action_type = 'cr' THEN amount
end, 0)) - Sum(Coalesce(CASE
WHEN action_type = 'dr' THEN
amount
end, 0)) > 0, 'dr', 'cr') AS balance
FROM tbl_balancesheet
GROUP BY client_id
HAVING balance = 'dr' AND total_debtors <> 0
仅获得信用证:
SELECT client_id,
Sum(Coalesce(CASE
WHEN action_type = 'dr' THEN amount
end, 0)) AS total_debits,
Sum(Coalesce(CASE
WHEN action_type = 'cr' THEN amount
end, 0)) AS total_credits,
Sum(Coalesce(CASE
WHEN action_type = 'cr' THEN amount
end, 0)) - Sum(Coalesce(CASE
WHEN action_type = 'dr' THEN
amount
end, 0)) AS total_debtors,
IF(Sum(Coalesce(CASE
WHEN action_type = 'cr' THEN amount
end, 0)) - Sum(Coalesce(CASE
WHEN action_type = 'dr' THEN
amount
end, 0)) > 0, 'dr', 'cr') AS balance
FROM tbl_balancesheet
GROUP BY client_id
HAVING balance = 'cr' AND total_debtors <> 0
答案 1 :(得分:0)
如果我正确理解您的问题,则需要一个简单的WHERE语句。因此,您的查询应该是这样的:
SELECT *, client_id
, SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debits
, SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) total_credits
, SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debtors
, SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) balance
FROM tbl_balancesheet
WHERE `action_type` = 'cr'
GROUP
BY client_id
HAVING balance <> 0
或者,不使用action_type列,而是使用计算出的列:
SELECT *, client_id
, SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debits
, SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) total_credits
, SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) total_debtors
, SUM(COALESCE(CASE WHEN action_type = 'dr' THEN amount END,0)) - SUM(COALESCE(CASE WHEN action_type = 'cr' THEN amount END,0)) balance
FROM tbl_balancesheet
WHERE `action_type` = 'cr'
GROUP
BY client_id
HAVING balance <> 0 AND total_credits > 0
第二个查询将返回所有拥有信用的人(因此为债权人)。这样行吗?