我有表事务:
id user_id amount tranx_type deposit_id
1 1 23 4 1
2 2 34 3
3 1 17 4 3
4 3 11 2
5 1 44 4 5
6 2 30 1
然后表存款
id user_id currency
1 1 usd
2 2 usd
3 1 eur
4 3 eur
5 1 usd
6 3 eur
我们需要获取 user_id=1 所存入的“usd”金额的总和。
表交易中的deposit_id
是表存款中的id
。
tranx_type
=4表示存款。
在这个例子中,结果是67。
到目前为止我正在尝试这个:
SELECT SUM( transactions.amount ) as total_amount
FROM transactions INNER JOIN deposits
ON transactions.deposit_id = deposits.id
WHERE transactions.user_id = "1" AND transactions.tranx_type = 4
AND deposits.currency = "usd"
但运气不好,我觉得我很接近,但我不确定为什么查询不起作用。
感谢任何帮助!
最佳答案
关于mysql - 2 个表的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47011280/