有表1如下
id | name | money | time |
---|---|---|---|
1 | mike | 6 | 2016-09-01 |
2 | mike | 648 | 2016-09-01 |
3 | leo | 488 | 2016-09-02 |
4 | mike | 6 | 2016-09-03 |
如何获得以下表2内容
id | name | amount | time |
---|---|---|---|
1 | mike | 6 | 2016-09-01 |
2 | mike | 654 | 2016-09-01 |
3 | leo | 488 | 2016-09-02 |
4 | mike | 660 | 2016-09-03 |
At present, I have also thought of a way. I don’t know if there is a better way to compare and learn from each other
Thank you for the invitation, try this~
Thanks for the invitation. Do you want to accumulate the amount field? Isn’t it possible to use user IDs for conditional accumulation?
You can take a look at the union keyword in sql
I feel that sql is not good at doing this kind of thing, and it would be better to put it in the code.
If there is one record per user, then
group by + sum
is enough.This is a stored procedure
This is the result, as shown below