Midnight Engineer Midnight Engineer - 2 months ago 6
MySQL Question

How to select and update referrer payments in this bitcoin faucet?

I am having problem in calculating the referral payout for a free bitcoin site. The table structure is as follows..


addr, bal, ref, pending, paid, ref_payment, earning


I am getting the all referral details with the following query...

select * from table where pending > 0 and ref != 'none'


I am getting the unique referral details with the following query...

select DISTINCT ref from table where pending > 0 and ref != 'none'


I am supposed to pay equal amount of pending to each referrer. Now I am stuck at two points that I have to do manually till now and is getting unmanageable with increasing data...


  1. Selection of all referrer and their referral amount, i.e. summation of all pending they have referred, in two column.

  2. Updating the table at a time so that referral amount gets added to existing ref_payment.



Any help to solve this problem is highly appreciated...

Answer

For your first question, I'd suggest something like this (depending on your schema)..

SELECT referrer,
    FORMAT(SUM(referral_amount),2) total 
FROM TableName
GROUP BY referrer
ORDER BY SUM(referral_amount) DESC;

The second one is a simple UPDATE query..

UPDATE table SET column = (SELECT other_column) // maybe with a WHERE
Comments