使用php查询帐户运行余额

时间:2017-07-16 18:04:45

标签: php mysql coalesce

请协助,如何为以下查询编写php代码:

SELECT transactions_id, trn_date, trn_dt_total, trn_ct_total, trn_description, trn_bank_reference, balance
FROM
(SELECT t.*, @n := IF(@g <> transactions_id, 0, @n) + 
COALESCE(trn_dt_total,0) - COALESCE(trn_ct_total, 0) balance, @g := 
transactions_id
FROM transactions t, (SELECT @n := 0) n, (SELECT @g := 0) g
WHERE trn_building_id = 1 and trn_unit_id = 1 and trn_tenant_id = 1
ORDER BY transactions_id, trn_date)
query

我的php页面查询

$details = $db->query();

在没有&#34;查询&#34;的情况下在MySql中运行查询时我得到错误:

1248 - Every derived table must have its own alias

1 个答案:

答案 0 :(得分:0)

错误是不言自明的。您应该为您创建的表命名别名。在括号中的子查询之后,您应该写as t_name,其中 tname 将是您的表别名

相关问题