简体   繁体   中英

mysql select two tables with userid and compare timestamp and count it

Hello I think I try here something complicated. Maybe you can help me out here.

I have two tables: earnings and payouts.

Earnings has userid, amount, timestamp as datetime and other stuff. It has just information when user was earning something. Example:

id | user_id | amount | timestamp  |
1  | 2       | 1050   | 31days ago | 
2  | 1       | 20     | 10days ago | 
3  | 1       | 10     | 9 days ago | 
4  | 2       | 10000  | 9 days ago | 
...

Payouts has userid, amount, timestamp as datetime and has entries about payouts if a user is above x earnings lets say 1000. Example

id | user_id | payout_amount | timestamp  |
1  | 2       | 1050          | 30days ago | 
...

To my problem now. I want to COUNT how many payouts are NOT done (who has no entry in payout). This means. I need to compare payouts.timestamp with earnings.timestamp which has same userid and check if there are newer entries then the payouts. If yes then count it how many (so I think here its needed to sum first the earnings). I am not even sure if this is possible.

I can do it also with php if this isn't possible alone with mysql.

For example the result should be: count = 2 because userid 1 has just 30 earnings so he didn't reach the 1000 also he has no entry in payouts table. Userid 2 has 10000 but he still has no payouts because we didn't execute it or make a entry in the payouts table. he just has a old payout and the new earnings isn't paid.

Edit: the 10days ago things are just example. I use there real datetime types EDIT2: Forget to say I tried this one:

select COUNT(e.amount) FROM earnings e, payouts p where p.payout_timestamp < e.timestamp AND p.user_id = e.user_id GROUP BY p.user_id, e.user_id

and go this:

| Count(e.amount) |
| 2               |
| 1               |

I think that comparing timestamps is not the only way to get result that you need. If these to tables correctly represent history of earnings and payments then you may just sum up total earnings and payments for each user and compare them counting every user_id that has less payments than earnings. For example:

SELECT user_id, SUM(amount) as amount FROM earnings GROUP BY user_id;

sums up earnings for each user,

SELECT user_id, SUM(payout_amount) as amount FROM payouts GROUP BY user_id;

sums up payments for each user.

Now left join them and count users who has less payments than earnings:

SELECT COUNT(e1.user_id) FROM (SELECT user_id, SUM(amount) as amount FROM earnings GROUP BY user_id) as e1 LEFT JOIN (SELECT user_id, SUM(payout_amount) as amount FROM payouts GROUP BY user_id) as p1 ON e1.amount > p1.amount;

For your tables example my result was:

+-------------------+
| COUNT(e1.user_id) |
+-------------------+
|                 2 |
+-------------------+

And to find users with payments not done just use the same query without COUNT() function:

+---------+
| user_id |
+---------+
|       2 |
|       1 |
+---------+

In my opinion this way is more stable because it is possible to have recent payments with less amount than total user earnings at the moment of payment, eg:

1) user earned 1000

2) then user earned 2000

3) and after that user been payed 1000

In this situation comparing timestamps without comparing amounts of payments will show you that this user has no payments to be done whereas you still need to pay him 2000.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM