So, I save all transactions in the transaction table with the following line:
+----------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+---------------+------+-----+---------+----------------+ | debit_amount | decimal(10,2) | YES | | 0.00 | | | credit_amount | decimal(10,2) | YES | | 0.00 | | | flag | int(11) | YES | | NULL | | | date | datetime | YES | | NULL | | | id | int(11) | NO | PRI | NULL | auto_increment | +----------------+---------------+------+-----+---------+----------------+
Then I save the total number of credits that the user has in the "credits" line in the user table.
I’m trying to find out if there is a discrepancy in the total amount (debit amount + loan amount) for each user stored in the transaction table to the number of loans stored in the user table.
mainly for each user
transactions.debit_amount + transactions.credit amount MUST EQUAL user.credits
but an operator that is not equal to the operator in the mysql query does not work (especially when the value of transaction.total is null, i.e. there is no row in the transaction table for this user):
SELECT s.id AS uid, s.total, s.credits FROM ( SELECT (sum(t.credit_amount) + sum(t.debit_amount)) AS total, t.userid, u.credits, u.id FROM transactions AS t RIGHT JOIN users AS u ON t.userid = u.id GROUP BY u.id ) AS s WHERE s.total != s.credits
user816604
source share