Accounts Table
user_id description credit debit created modified
01 Earned 10 0 06-11-2013 06-11-2013
02 Normal 0 1 05-11-2013 05-11-2013
03 Earned 5 0 04-11-2013 04-11-2013
04 Earned 20 0 03-11-2013 03-11-2013
05 Normal 0 1 02-11-2013 02-11-2013
01 Earned 5 0 02-11-2013 02-11-2013
01 Normal 0 1 01-11-2013 01-11-2013
02 Earned 10 0 01-11-2013 01-11-2013
I tried following Queries:
SELECT SUM(CASE WHEN description LIKE '%Earned%'
THEN Credit
ELSE 0
END ) as cre, SUM(debit) as deb FROM accounts
WHERE created is between '$Monday' AND '$Sunday'
INSERT into accounts (user_id, description, credit, debit, created, modified)
VALUES ( 'user_id', 'Removed Free Earned', 'cre', 'deb', Now(), Now());
I want to Select Account table and Sum credit and Sum debit according to users for example if user_id 01 Total credit is 10 and Total debit in last Monday to Sunday is 3 then 7 points should be added in Insert Query place of 'deb'. But If Total credit is 10 and total debit is 20, then no points will be added mean if debit is greater than credit.
I want Select and Insert Query should be written as one query.